xxan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Wing Commander/Xan Video Decoder
  3. * Copyright (C) 2011 Konstantin Shishkov
  4. * based on work by Mike Melanson
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avcodec.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/mem.h"
  25. #include "bytestream.h"
  26. #define BITSTREAM_READER_LE
  27. #include "get_bits.h"
  28. typedef struct XanContext {
  29. AVCodecContext *avctx;
  30. AVFrame pic;
  31. uint8_t *y_buffer;
  32. uint8_t *scratch_buffer;
  33. int buffer_size;
  34. GetByteContext gb;
  35. } XanContext;
  36. static av_cold int xan_decode_init(AVCodecContext *avctx)
  37. {
  38. XanContext *s = avctx->priv_data;
  39. s->avctx = avctx;
  40. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  41. s->buffer_size = avctx->width * avctx->height;
  42. s->y_buffer = av_malloc(s->buffer_size);
  43. if (!s->y_buffer)
  44. return AVERROR(ENOMEM);
  45. s->scratch_buffer = av_malloc(s->buffer_size + 130);
  46. if (!s->scratch_buffer) {
  47. av_freep(&s->y_buffer);
  48. return AVERROR(ENOMEM);
  49. }
  50. return 0;
  51. }
  52. static int xan_unpack_luma(XanContext *s,
  53. uint8_t *dst, const int dst_size)
  54. {
  55. int tree_size, eof;
  56. int bits, mask;
  57. int tree_root, node;
  58. const uint8_t *dst_end = dst + dst_size;
  59. GetByteContext tree = s->gb;
  60. int start_off = bytestream2_tell(&tree);
  61. tree_size = bytestream2_get_byte(&s->gb);
  62. eof = bytestream2_get_byte(&s->gb);
  63. tree_root = eof + tree_size;
  64. bytestream2_skip(&s->gb, tree_size * 2);
  65. node = tree_root;
  66. bits = bytestream2_get_byte(&s->gb);
  67. mask = 0x80;
  68. for (;;) {
  69. int bit = !!(bits & mask);
  70. mask >>= 1;
  71. bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
  72. node = bytestream2_get_byte(&tree);
  73. if (node == eof)
  74. break;
  75. if (node < eof) {
  76. *dst++ = node;
  77. if (dst > dst_end)
  78. break;
  79. node = tree_root;
  80. }
  81. if (!mask) {
  82. if (bytestream2_get_bytes_left(&s->gb) <= 0)
  83. break;
  84. bits = bytestream2_get_byteu(&s->gb);
  85. mask = 0x80;
  86. }
  87. }
  88. return dst != dst_end ? AVERROR_INVALIDDATA : 0;
  89. }
  90. /* almost the same as in xan_wc3 decoder */
  91. static int xan_unpack(XanContext *s,
  92. uint8_t *dest, const int dest_len)
  93. {
  94. uint8_t opcode;
  95. int size;
  96. uint8_t *orig_dest = dest;
  97. const uint8_t *dest_end = dest + dest_len;
  98. while (dest < dest_end) {
  99. if (bytestream2_get_bytes_left(&s->gb) <= 0)
  100. return AVERROR_INVALIDDATA;
  101. opcode = bytestream2_get_byteu(&s->gb);
  102. if (opcode < 0xe0) {
  103. int size2, back;
  104. if ((opcode & 0x80) == 0) {
  105. size = opcode & 3;
  106. back = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->gb) + 1;
  107. size2 = ((opcode & 0x1c) >> 2) + 3;
  108. } else if ((opcode & 0x40) == 0) {
  109. size = bytestream2_peek_byte(&s->gb) >> 6;
  110. back = (bytestream2_get_be16(&s->gb) & 0x3fff) + 1;
  111. size2 = (opcode & 0x3f) + 4;
  112. } else {
  113. size = opcode & 3;
  114. back = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->gb) + 1;
  115. size2 = ((opcode & 0x0c) << 6) + bytestream2_get_byte(&s->gb) + 5;
  116. if (size + size2 > dest_end - dest)
  117. break;
  118. }
  119. if (dest + size + size2 > dest_end ||
  120. dest - orig_dest + size < back)
  121. return AVERROR_INVALIDDATA;
  122. bytestream2_get_buffer(&s->gb, dest, size);
  123. dest += size;
  124. av_memcpy_backptr(dest, back, size2);
  125. dest += size2;
  126. } else {
  127. int finish = opcode >= 0xfc;
  128. size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
  129. if (dest_end - dest < size)
  130. return AVERROR_INVALIDDATA;
  131. bytestream2_get_buffer(&s->gb, dest, size);
  132. dest += size;
  133. if (finish)
  134. break;
  135. }
  136. }
  137. return dest - orig_dest;
  138. }
  139. static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
  140. {
  141. XanContext *s = avctx->priv_data;
  142. uint8_t *U, *V;
  143. int val, uval, vval;
  144. int i, j;
  145. const uint8_t *src, *src_end;
  146. const uint8_t *table;
  147. int mode, offset, dec_size, table_size;
  148. if (!chroma_off)
  149. return 0;
  150. if (chroma_off + 4 >= bytestream2_get_bytes_left(&s->gb)) {
  151. av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
  152. return AVERROR_INVALIDDATA;
  153. }
  154. bytestream2_seek(&s->gb, chroma_off + 4, SEEK_SET);
  155. mode = bytestream2_get_le16(&s->gb);
  156. table = s->gb.buffer;
  157. table_size = bytestream2_get_le16(&s->gb);
  158. offset = table_size * 2;
  159. table_size += 1;
  160. if (offset >= bytestream2_get_bytes_left(&s->gb)) {
  161. av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
  162. return AVERROR_INVALIDDATA;
  163. }
  164. bytestream2_skip(&s->gb, offset);
  165. memset(s->scratch_buffer, 0, s->buffer_size);
  166. dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
  167. if (dec_size < 0) {
  168. av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
  169. return AVERROR_INVALIDDATA;
  170. }
  171. U = s->pic.data[1];
  172. V = s->pic.data[2];
  173. src = s->scratch_buffer;
  174. src_end = src + dec_size;
  175. if (mode) {
  176. for (j = 0; j < avctx->height >> 1; j++) {
  177. for (i = 0; i < avctx->width >> 1; i++) {
  178. if (src_end - src < 1)
  179. return 0;
  180. val = *src++;
  181. if (val) {
  182. if (val >= table_size)
  183. return AVERROR_INVALIDDATA;
  184. val = AV_RL16(table + (val << 1));
  185. uval = (val >> 3) & 0xF8;
  186. vval = (val >> 8) & 0xF8;
  187. U[i] = uval | (uval >> 5);
  188. V[i] = vval | (vval >> 5);
  189. }
  190. }
  191. U += s->pic.linesize[1];
  192. V += s->pic.linesize[2];
  193. }
  194. } else {
  195. uint8_t *U2 = U + s->pic.linesize[1];
  196. uint8_t *V2 = V + s->pic.linesize[2];
  197. for (j = 0; j < avctx->height >> 2; j++) {
  198. for (i = 0; i < avctx->width >> 1; i += 2) {
  199. if (src_end - src < 1)
  200. return 0;
  201. val = *src++;
  202. if (val) {
  203. if (val >= table_size)
  204. return AVERROR_INVALIDDATA;
  205. val = AV_RL16(table + (val << 1));
  206. uval = (val >> 3) & 0xF8;
  207. vval = (val >> 8) & 0xF8;
  208. U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
  209. V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
  210. }
  211. }
  212. U += s->pic.linesize[1] * 2;
  213. V += s->pic.linesize[2] * 2;
  214. U2 += s->pic.linesize[1] * 2;
  215. V2 += s->pic.linesize[2] * 2;
  216. }
  217. }
  218. return 0;
  219. }
  220. static int xan_decode_frame_type0(AVCodecContext *avctx)
  221. {
  222. XanContext *s = avctx->priv_data;
  223. uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
  224. unsigned chroma_off, corr_off;
  225. int cur, last;
  226. int i, j;
  227. int ret;
  228. chroma_off = bytestream2_get_le32(&s->gb);
  229. corr_off = bytestream2_get_le32(&s->gb);
  230. if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
  231. return ret;
  232. if (corr_off >= bytestream2_size(&s->gb)) {
  233. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
  234. corr_off = 0;
  235. }
  236. bytestream2_seek(&s->gb, 12, SEEK_SET);
  237. ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
  238. if (ret) {
  239. av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
  240. return ret;
  241. }
  242. ybuf = s->y_buffer;
  243. last = *src++;
  244. ybuf[0] = last << 1;
  245. for (j = 1; j < avctx->width - 1; j += 2) {
  246. cur = (last + *src++) & 0x1F;
  247. ybuf[j] = last + cur;
  248. ybuf[j+1] = cur << 1;
  249. last = cur;
  250. }
  251. ybuf[j] = last << 1;
  252. prev_buf = ybuf;
  253. ybuf += avctx->width;
  254. for (i = 1; i < avctx->height; i++) {
  255. last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
  256. ybuf[0] = last << 1;
  257. for (j = 1; j < avctx->width - 1; j += 2) {
  258. cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
  259. ybuf[j] = last + cur;
  260. ybuf[j+1] = cur << 1;
  261. last = cur;
  262. }
  263. ybuf[j] = last << 1;
  264. prev_buf = ybuf;
  265. ybuf += avctx->width;
  266. }
  267. if (corr_off) {
  268. int dec_size;
  269. bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
  270. dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
  271. if (dec_size < 0)
  272. dec_size = 0;
  273. else
  274. dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
  275. for (i = 0; i < dec_size; i++)
  276. s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
  277. }
  278. src = s->y_buffer;
  279. ybuf = s->pic.data[0];
  280. for (j = 0; j < avctx->height; j++) {
  281. for (i = 0; i < avctx->width; i++)
  282. ybuf[i] = (src[i] << 2) | (src[i] >> 3);
  283. src += avctx->width;
  284. ybuf += s->pic.linesize[0];
  285. }
  286. return 0;
  287. }
  288. static int xan_decode_frame_type1(AVCodecContext *avctx)
  289. {
  290. XanContext *s = avctx->priv_data;
  291. uint8_t *ybuf, *src = s->scratch_buffer;
  292. int cur, last;
  293. int i, j;
  294. int ret;
  295. if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
  296. return ret;
  297. bytestream2_seek(&s->gb, 16, SEEK_SET);
  298. ret = xan_unpack_luma(s, src,
  299. s->buffer_size >> 1);
  300. if (ret) {
  301. av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
  302. return ret;
  303. }
  304. ybuf = s->y_buffer;
  305. for (i = 0; i < avctx->height; i++) {
  306. last = (ybuf[0] + (*src++ << 1)) & 0x3F;
  307. ybuf[0] = last;
  308. for (j = 1; j < avctx->width - 1; j += 2) {
  309. cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
  310. ybuf[j] = (last + cur) >> 1;
  311. ybuf[j+1] = cur;
  312. last = cur;
  313. }
  314. ybuf[j] = last;
  315. ybuf += avctx->width;
  316. }
  317. src = s->y_buffer;
  318. ybuf = s->pic.data[0];
  319. for (j = 0; j < avctx->height; j++) {
  320. for (i = 0; i < avctx->width; i++)
  321. ybuf[i] = (src[i] << 2) | (src[i] >> 3);
  322. src += avctx->width;
  323. ybuf += s->pic.linesize[0];
  324. }
  325. return 0;
  326. }
  327. static int xan_decode_frame(AVCodecContext *avctx,
  328. void *data, int *data_size,
  329. AVPacket *avpkt)
  330. {
  331. XanContext *s = avctx->priv_data;
  332. int ftype;
  333. int ret;
  334. s->pic.reference = 3;
  335. s->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
  336. FF_BUFFER_HINTS_PRESERVE |
  337. FF_BUFFER_HINTS_REUSABLE;
  338. if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
  339. av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  340. return ret;
  341. }
  342. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  343. ftype = bytestream2_get_le32(&s->gb);
  344. switch (ftype) {
  345. case 0:
  346. ret = xan_decode_frame_type0(avctx);
  347. break;
  348. case 1:
  349. ret = xan_decode_frame_type1(avctx);
  350. break;
  351. default:
  352. av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
  353. return AVERROR_INVALIDDATA;
  354. }
  355. if (ret)
  356. return ret;
  357. *data_size = sizeof(AVFrame);
  358. *(AVFrame*)data = s->pic;
  359. return avpkt->size;
  360. }
  361. static av_cold int xan_decode_end(AVCodecContext *avctx)
  362. {
  363. XanContext *s = avctx->priv_data;
  364. if (s->pic.data[0])
  365. avctx->release_buffer(avctx, &s->pic);
  366. av_freep(&s->y_buffer);
  367. av_freep(&s->scratch_buffer);
  368. return 0;
  369. }
  370. AVCodec ff_xan_wc4_decoder = {
  371. .name = "xan_wc4",
  372. .type = AVMEDIA_TYPE_VIDEO,
  373. .id = AV_CODEC_ID_XAN_WC4,
  374. .priv_data_size = sizeof(XanContext),
  375. .init = xan_decode_init,
  376. .close = xan_decode_end,
  377. .decode = xan_decode_frame,
  378. .capabilities = CODEC_CAP_DR1,
  379. .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
  380. };