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.

423 lines
12KB

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