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.

422 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 "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 -1;
  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 -1;
  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 -1;
  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 -1;
  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 -1;
  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. val = *src++;
  179. if (val && val < table_size) {
  180. val = AV_RL16(table + (val << 1));
  181. uval = (val >> 3) & 0xF8;
  182. vval = (val >> 8) & 0xF8;
  183. U[i] = uval | (uval >> 5);
  184. V[i] = vval | (vval >> 5);
  185. }
  186. if (src == src_end)
  187. return 0;
  188. }
  189. U += s->pic.linesize[1];
  190. V += s->pic.linesize[2];
  191. }
  192. } else {
  193. uint8_t *U2 = U + s->pic.linesize[1];
  194. uint8_t *V2 = V + s->pic.linesize[2];
  195. for (j = 0; j < avctx->height >> 2; j++) {
  196. for (i = 0; i < avctx->width >> 1; i += 2) {
  197. val = *src++;
  198. if (val && val < table_size) {
  199. val = AV_RL16(table + (val << 1));
  200. uval = (val >> 3) & 0xF8;
  201. vval = (val >> 8) & 0xF8;
  202. U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
  203. V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
  204. }
  205. }
  206. U += s->pic.linesize[1] * 2;
  207. V += s->pic.linesize[2] * 2;
  208. U2 += s->pic.linesize[1] * 2;
  209. V2 += s->pic.linesize[2] * 2;
  210. }
  211. }
  212. return 0;
  213. }
  214. static int xan_decode_frame_type0(AVCodecContext *avctx)
  215. {
  216. XanContext *s = avctx->priv_data;
  217. uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
  218. unsigned chroma_off, corr_off;
  219. int cur, last;
  220. int i, j;
  221. int ret;
  222. chroma_off = bytestream2_get_le32(&s->gb);
  223. corr_off = bytestream2_get_le32(&s->gb);
  224. if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
  225. return ret;
  226. if (corr_off >= (s->gb.buffer_end - s->gb.buffer_start)) {
  227. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
  228. corr_off = 0;
  229. }
  230. bytestream2_seek(&s->gb, 12, SEEK_SET);
  231. ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
  232. if (ret) {
  233. av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
  234. return ret;
  235. }
  236. ybuf = s->y_buffer;
  237. last = *src++;
  238. ybuf[0] = last << 1;
  239. for (j = 1; j < avctx->width - 1; j += 2) {
  240. cur = (last + *src++) & 0x1F;
  241. ybuf[j] = last + cur;
  242. ybuf[j+1] = cur << 1;
  243. last = cur;
  244. }
  245. ybuf[j] = last << 1;
  246. prev_buf = ybuf;
  247. ybuf += avctx->width;
  248. for (i = 1; i < avctx->height; i++) {
  249. last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
  250. ybuf[0] = last << 1;
  251. for (j = 1; j < avctx->width - 1; j += 2) {
  252. cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
  253. ybuf[j] = last + cur;
  254. ybuf[j+1] = cur << 1;
  255. last = cur;
  256. }
  257. ybuf[j] = last << 1;
  258. prev_buf = ybuf;
  259. ybuf += avctx->width;
  260. }
  261. if (corr_off) {
  262. int dec_size;
  263. bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
  264. dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
  265. if (dec_size < 0)
  266. dec_size = 0;
  267. for (i = 0; i < dec_size; i++)
  268. s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
  269. }
  270. src = s->y_buffer;
  271. ybuf = s->pic.data[0];
  272. for (j = 0; j < avctx->height; j++) {
  273. for (i = 0; i < avctx->width; i++)
  274. ybuf[i] = (src[i] << 2) | (src[i] >> 3);
  275. src += avctx->width;
  276. ybuf += s->pic.linesize[0];
  277. }
  278. return 0;
  279. }
  280. static int xan_decode_frame_type1(AVCodecContext *avctx)
  281. {
  282. XanContext *s = avctx->priv_data;
  283. uint8_t *ybuf, *src = s->scratch_buffer;
  284. int cur, last;
  285. int i, j;
  286. int ret;
  287. if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
  288. return ret;
  289. bytestream2_seek(&s->gb, 16, SEEK_SET);
  290. ret = xan_unpack_luma(s, src,
  291. s->buffer_size >> 1);
  292. if (ret) {
  293. av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
  294. return ret;
  295. }
  296. ybuf = s->y_buffer;
  297. for (i = 0; i < avctx->height; i++) {
  298. last = (ybuf[0] + (*src++ << 1)) & 0x3F;
  299. ybuf[0] = last;
  300. for (j = 1; j < avctx->width - 1; j += 2) {
  301. cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
  302. ybuf[j] = (last + cur) >> 1;
  303. ybuf[j+1] = cur;
  304. last = cur;
  305. }
  306. ybuf[j] = last;
  307. ybuf += avctx->width;
  308. }
  309. src = s->y_buffer;
  310. ybuf = s->pic.data[0];
  311. for (j = 0; j < avctx->height; j++) {
  312. for (i = 0; i < avctx->width; i++)
  313. ybuf[i] = (src[i] << 2) | (src[i] >> 3);
  314. src += avctx->width;
  315. ybuf += s->pic.linesize[0];
  316. }
  317. return 0;
  318. }
  319. static int xan_decode_frame(AVCodecContext *avctx,
  320. void *data, int *data_size,
  321. AVPacket *avpkt)
  322. {
  323. XanContext *s = avctx->priv_data;
  324. int ftype;
  325. int ret;
  326. s->pic.reference = 1;
  327. s->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
  328. FF_BUFFER_HINTS_PRESERVE |
  329. FF_BUFFER_HINTS_REUSABLE;
  330. if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
  331. av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  332. return ret;
  333. }
  334. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  335. ftype = bytestream2_get_le32(&s->gb);
  336. switch (ftype) {
  337. case 0:
  338. ret = xan_decode_frame_type0(avctx);
  339. break;
  340. case 1:
  341. ret = xan_decode_frame_type1(avctx);
  342. break;
  343. default:
  344. av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
  345. return -1;
  346. }
  347. if (ret)
  348. return ret;
  349. *data_size = sizeof(AVFrame);
  350. *(AVFrame*)data = s->pic;
  351. return avpkt->size;
  352. }
  353. static av_cold int xan_decode_end(AVCodecContext *avctx)
  354. {
  355. XanContext *s = avctx->priv_data;
  356. if (s->pic.data[0])
  357. avctx->release_buffer(avctx, &s->pic);
  358. av_freep(&s->y_buffer);
  359. av_freep(&s->scratch_buffer);
  360. return 0;
  361. }
  362. AVCodec ff_xan_wc4_decoder = {
  363. .name = "xan_wc4",
  364. .type = AVMEDIA_TYPE_VIDEO,
  365. .id = AV_CODEC_ID_XAN_WC4,
  366. .priv_data_size = sizeof(XanContext),
  367. .init = xan_decode_init,
  368. .close = xan_decode_end,
  369. .decode = xan_decode_frame,
  370. .capabilities = CODEC_CAP_DR1,
  371. .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
  372. };