dsicinav.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Delphine Software International CIN Audio/Video Decoders
  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 libavcodec/dsicinav.c
  23. * Delphine Software International CIN audio/video decoders
  24. */
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. typedef enum CinVideoBitmapIndex {
  28. CIN_CUR_BMP = 0, /* current */
  29. CIN_PRE_BMP = 1, /* previous */
  30. CIN_INT_BMP = 2 /* intermediate */
  31. } CinVideoBitmapIndex;
  32. typedef struct CinVideoContext {
  33. AVCodecContext *avctx;
  34. AVFrame frame;
  35. unsigned int bitmap_size;
  36. uint32_t palette[256];
  37. uint8_t *bitmap_table[3];
  38. } CinVideoContext;
  39. typedef struct CinAudioContext {
  40. AVCodecContext *avctx;
  41. int initial_decode_frame;
  42. int delta;
  43. } CinAudioContext;
  44. /* table defining a geometric sequence with multiplier = 32767 ^ (1 / 128) */
  45. static const int16_t cinaudio_delta16_table[256] = {
  46. 0, 0, 0, 0, 0, 0, 0, 0,
  47. 0, 0, 0, 0, 0, 0, 0, 0,
  48. 0, 0, 0, -30210, -27853, -25680, -23677, -21829,
  49. -20126, -18556, -17108, -15774, -14543, -13408, -12362, -11398,
  50. -10508, -9689, -8933, -8236, -7593, -7001, -6455, -5951,
  51. -5487, -5059, -4664, -4300, -3964, -3655, -3370, -3107,
  52. -2865, -2641, -2435, -2245, -2070, -1908, -1759, -1622,
  53. -1495, -1379, -1271, -1172, -1080, -996, -918, -847,
  54. -781, -720, -663, -612, -564, -520, -479, -442,
  55. -407, -376, -346, -319, -294, -271, -250, -230,
  56. -212, -196, -181, -166, -153, -141, -130, -120,
  57. -111, -102, -94, -87, -80, -74, -68, -62,
  58. -58, -53, -49, -45, -41, -38, -35, -32,
  59. -30, -27, -25, -23, -21, -20, -18, -17,
  60. -15, -14, -13, -12, -11, -10, -9, -8,
  61. -7, -6, -5, -4, -3, -2, -1, 0,
  62. 0, 1, 2, 3, 4, 5, 6, 7,
  63. 8, 9, 10, 11, 12, 13, 14, 15,
  64. 17, 18, 20, 21, 23, 25, 27, 30,
  65. 32, 35, 38, 41, 45, 49, 53, 58,
  66. 62, 68, 74, 80, 87, 94, 102, 111,
  67. 120, 130, 141, 153, 166, 181, 196, 212,
  68. 230, 250, 271, 294, 319, 346, 376, 407,
  69. 442, 479, 520, 564, 612, 663, 720, 781,
  70. 847, 918, 996, 1080, 1172, 1271, 1379, 1495,
  71. 1622, 1759, 1908, 2070, 2245, 2435, 2641, 2865,
  72. 3107, 3370, 3655, 3964, 4300, 4664, 5059, 5487,
  73. 5951, 6455, 7001, 7593, 8236, 8933, 9689, 10508,
  74. 11398, 12362, 13408, 14543, 15774, 17108, 18556, 20126,
  75. 21829, 23677, 25680, 27853, 30210, 0, 0, 0,
  76. 0, 0, 0, 0, 0, 0, 0, 0,
  77. 0, 0, 0, 0, 0, 0, 0, 0
  78. };
  79. static av_cold int cinvideo_decode_init(AVCodecContext *avctx)
  80. {
  81. CinVideoContext *cin = avctx->priv_data;
  82. unsigned int i;
  83. cin->avctx = avctx;
  84. avctx->pix_fmt = PIX_FMT_PAL8;
  85. cin->frame.data[0] = NULL;
  86. cin->bitmap_size = avctx->width * avctx->height;
  87. for (i = 0; i < 3; ++i) {
  88. cin->bitmap_table[i] = av_mallocz(cin->bitmap_size);
  89. if (!cin->bitmap_table[i])
  90. av_log(avctx, AV_LOG_ERROR, "Can't allocate bitmap buffers.\n");
  91. }
  92. return 0;
  93. }
  94. static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst, int size)
  95. {
  96. while (size--)
  97. *dst++ += *src++;
  98. }
  99. static int cin_decode_huffman(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
  100. {
  101. int b, huff_code = 0;
  102. unsigned char huff_code_table[15];
  103. unsigned char *dst_cur = dst;
  104. unsigned char *dst_end = dst + dst_size;
  105. const unsigned char *src_end = src + src_size;
  106. memcpy(huff_code_table, src, 15); src += 15; src_size -= 15;
  107. while (src < src_end) {
  108. huff_code = *src++;
  109. if ((huff_code >> 4) == 15) {
  110. b = huff_code << 4;
  111. huff_code = *src++;
  112. *dst_cur++ = b | (huff_code >> 4);
  113. } else
  114. *dst_cur++ = huff_code_table[huff_code >> 4];
  115. if (dst_cur >= dst_end)
  116. break;
  117. huff_code &= 15;
  118. if (huff_code == 15) {
  119. *dst_cur++ = *src++;
  120. } else
  121. *dst_cur++ = huff_code_table[huff_code];
  122. if (dst_cur >= dst_end)
  123. break;
  124. }
  125. return dst_cur - dst;
  126. }
  127. static void cin_decode_lzss(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
  128. {
  129. uint16_t cmd;
  130. int i, sz, offset, code;
  131. unsigned char *dst_end = dst + dst_size;
  132. const unsigned char *src_end = src + src_size;
  133. while (src < src_end && dst < dst_end) {
  134. code = *src++;
  135. for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) {
  136. if (code & (1 << i)) {
  137. *dst++ = *src++;
  138. } else {
  139. cmd = AV_RL16(src); src += 2;
  140. offset = cmd >> 4;
  141. sz = (cmd & 0xF) + 2;
  142. /* don't use memcpy/memmove here as the decoding routine (ab)uses */
  143. /* buffer overlappings to repeat bytes in the destination */
  144. sz = FFMIN(sz, dst_end - dst);
  145. while (sz--) {
  146. *dst = *(dst - offset - 1);
  147. ++dst;
  148. }
  149. }
  150. }
  151. }
  152. }
  153. static void cin_decode_rle(const unsigned char *src, int src_size, unsigned char *dst, int dst_size)
  154. {
  155. int len, code;
  156. unsigned char *dst_end = dst + dst_size;
  157. const unsigned char *src_end = src + src_size;
  158. while (src < src_end && dst < dst_end) {
  159. code = *src++;
  160. if (code & 0x80) {
  161. len = code - 0x7F;
  162. memset(dst, *src++, FFMIN(len, dst_end - dst));
  163. } else {
  164. len = code + 1;
  165. memcpy(dst, src, FFMIN(len, dst_end - dst));
  166. src += len;
  167. }
  168. dst += len;
  169. }
  170. }
  171. static int cinvideo_decode_frame(AVCodecContext *avctx,
  172. void *data, int *data_size,
  173. const uint8_t *buf, int buf_size)
  174. {
  175. CinVideoContext *cin = avctx->priv_data;
  176. int i, y, palette_type, palette_colors_count, bitmap_frame_type, bitmap_frame_size;
  177. cin->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  178. if (avctx->reget_buffer(avctx, &cin->frame)) {
  179. av_log(cin->avctx, AV_LOG_ERROR, "delphinecinvideo: reget_buffer() failed to allocate a frame\n");
  180. return -1;
  181. }
  182. palette_type = buf[0];
  183. palette_colors_count = AV_RL16(buf+1);
  184. bitmap_frame_type = buf[3];
  185. buf += 4;
  186. bitmap_frame_size = buf_size - 4;
  187. /* handle palette */
  188. if (palette_type == 0) {
  189. if (palette_colors_count > 256)
  190. return AVERROR_INVALIDDATA;
  191. for (i = 0; i < palette_colors_count; ++i) {
  192. cin->palette[i] = bytestream_get_le24(&buf);
  193. bitmap_frame_size -= 3;
  194. }
  195. } else {
  196. for (i = 0; i < palette_colors_count; ++i) {
  197. cin->palette[buf[0]] = AV_RL24(buf+1);
  198. buf += 4;
  199. bitmap_frame_size -= 4;
  200. }
  201. }
  202. memcpy(cin->frame.data[1], cin->palette, sizeof(cin->palette));
  203. cin->frame.palette_has_changed = 1;
  204. /* note: the decoding routines below assumes that surface.width = surface.pitch */
  205. switch (bitmap_frame_type) {
  206. case 9:
  207. cin_decode_rle(buf, bitmap_frame_size,
  208. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  209. break;
  210. case 34:
  211. cin_decode_rle(buf, bitmap_frame_size,
  212. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  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. cin_decode_huffman(buf, bitmap_frame_size,
  218. cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
  219. cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
  220. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  221. break;
  222. case 36:
  223. bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
  224. cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
  225. cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
  226. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  227. cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
  228. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  229. break;
  230. case 37:
  231. cin_decode_huffman(buf, bitmap_frame_size,
  232. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  233. break;
  234. case 38:
  235. cin_decode_lzss(buf, bitmap_frame_size,
  236. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  237. break;
  238. case 39:
  239. cin_decode_lzss(buf, bitmap_frame_size,
  240. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  241. cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
  242. cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
  243. break;
  244. }
  245. for (y = 0; y < cin->avctx->height; ++y)
  246. memcpy(cin->frame.data[0] + (cin->avctx->height - 1 - y) * cin->frame.linesize[0],
  247. cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
  248. cin->avctx->width);
  249. FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_table[CIN_PRE_BMP]);
  250. *data_size = sizeof(AVFrame);
  251. *(AVFrame *)data = cin->frame;
  252. return buf_size;
  253. }
  254. static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
  255. {
  256. CinVideoContext *cin = avctx->priv_data;
  257. int i;
  258. if (cin->frame.data[0])
  259. avctx->release_buffer(avctx, &cin->frame);
  260. for (i = 0; i < 3; ++i)
  261. av_free(cin->bitmap_table[i]);
  262. return 0;
  263. }
  264. static av_cold int cinaudio_decode_init(AVCodecContext *avctx)
  265. {
  266. CinAudioContext *cin = avctx->priv_data;
  267. cin->avctx = avctx;
  268. cin->initial_decode_frame = 1;
  269. cin->delta = 0;
  270. avctx->sample_fmt = SAMPLE_FMT_S16;
  271. return 0;
  272. }
  273. static int cinaudio_decode_frame(AVCodecContext *avctx,
  274. void *data, int *data_size,
  275. const uint8_t *buf, int buf_size)
  276. {
  277. CinAudioContext *cin = avctx->priv_data;
  278. const uint8_t *src = buf;
  279. int16_t *samples = (int16_t *)data;
  280. buf_size = FFMIN(buf_size, *data_size/2);
  281. if (cin->initial_decode_frame) {
  282. cin->initial_decode_frame = 0;
  283. cin->delta = (int16_t)AV_RL16(src); src += 2;
  284. *samples++ = cin->delta;
  285. buf_size -= 2;
  286. }
  287. while (buf_size > 0) {
  288. cin->delta += cinaudio_delta16_table[*src++];
  289. cin->delta = av_clip_int16(cin->delta);
  290. *samples++ = cin->delta;
  291. --buf_size;
  292. }
  293. *data_size = (uint8_t *)samples - (uint8_t *)data;
  294. return src - buf;
  295. }
  296. AVCodec dsicinvideo_decoder = {
  297. "dsicinvideo",
  298. CODEC_TYPE_VIDEO,
  299. CODEC_ID_DSICINVIDEO,
  300. sizeof(CinVideoContext),
  301. cinvideo_decode_init,
  302. NULL,
  303. cinvideo_decode_end,
  304. cinvideo_decode_frame,
  305. CODEC_CAP_DR1,
  306. .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
  307. };
  308. AVCodec dsicinaudio_decoder = {
  309. "dsicinaudio",
  310. CODEC_TYPE_AUDIO,
  311. CODEC_ID_DSICINAUDIO,
  312. sizeof(CinAudioContext),
  313. cinaudio_decode_init,
  314. NULL,
  315. NULL,
  316. cinaudio_decode_frame,
  317. .long_name = NULL_IF_CONFIG_SMALL("Delphine Software International CIN audio"),
  318. };