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.

431 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 ALT_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. } 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 = 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(const uint8_t *src, const int src_size,
  53. uint8_t *dst, const int dst_size)
  54. {
  55. int tree_size, eof;
  56. const uint8_t *tree;
  57. int bits, mask;
  58. int tree_root, node;
  59. const uint8_t *dst_end = dst + dst_size;
  60. const uint8_t *src_end = src + src_size;
  61. tree_size = *src++;
  62. eof = *src++;
  63. tree = src - eof * 2 - 2;
  64. tree_root = eof + tree_size;
  65. src += tree_size * 2;
  66. node = tree_root;
  67. bits = *src++;
  68. mask = 0x80;
  69. for (;;) {
  70. int bit = !!(bits & mask);
  71. mask >>= 1;
  72. node = tree[node*2 + bit];
  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. bits = *src++;
  83. if (src > src_end)
  84. break;
  85. mask = 0x80;
  86. }
  87. }
  88. return dst != dst_end;
  89. }
  90. /* almost the same as in xan_wc3 decoder */
  91. static int xan_unpack(uint8_t *dest, const int dest_len,
  92. const uint8_t *src, const int src_len)
  93. {
  94. uint8_t opcode;
  95. int size;
  96. uint8_t *orig_dest = dest;
  97. const uint8_t *src_end = src + src_len;
  98. const uint8_t *dest_end = dest + dest_len;
  99. while (dest < dest_end) {
  100. opcode = *src++;
  101. if (opcode < 0xe0) {
  102. int size2, back;
  103. if ((opcode & 0x80) == 0) {
  104. size = opcode & 3;
  105. back = ((opcode & 0x60) << 3) + *src++ + 1;
  106. size2 = ((opcode & 0x1c) >> 2) + 3;
  107. } else if ((opcode & 0x40) == 0) {
  108. size = *src >> 6;
  109. back = (bytestream_get_be16(&src) & 0x3fff) + 1;
  110. size2 = (opcode & 0x3f) + 4;
  111. } else {
  112. size = opcode & 3;
  113. back = ((opcode & 0x10) << 12) + bytestream_get_be16(&src) + 1;
  114. size2 = ((opcode & 0x0c) << 6) + *src++ + 5;
  115. if (size + size2 > dest_end - dest)
  116. break;
  117. }
  118. if (src + size > src_end || dest + size + size2 > dest_end ||
  119. dest - orig_dest + size < back)
  120. return -1;
  121. bytestream_get_buffer(&src, dest, size);
  122. dest += size;
  123. av_memcpy_backptr(dest, back, size2);
  124. dest += size2;
  125. } else {
  126. int finish = opcode >= 0xfc;
  127. size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
  128. if (src + size > src_end || dest + size > dest_end)
  129. return -1;
  130. bytestream_get_buffer(&src, dest, size);
  131. dest += size;
  132. if (finish)
  133. break;
  134. }
  135. }
  136. return dest - orig_dest;
  137. }
  138. static int xan_decode_chroma(AVCodecContext *avctx, AVPacket *avpkt)
  139. {
  140. const uint8_t *buf = avpkt->data;
  141. XanContext *s = avctx->priv_data;
  142. uint8_t *U, *V;
  143. unsigned chroma_off;
  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;
  149. chroma_off = AV_RL32(buf + 4);
  150. if (!chroma_off)
  151. return 0;
  152. if (chroma_off + 10 >= avpkt->size) {
  153. av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
  154. return -1;
  155. }
  156. src = avpkt->data + 4 + chroma_off;
  157. table = src + 2;
  158. mode = bytestream_get_le16(&src);
  159. offset = bytestream_get_le16(&src) * 2;
  160. if (src - avpkt->data >= avpkt->size - offset) {
  161. av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
  162. return -1;
  163. }
  164. memset(s->scratch_buffer, 0, s->buffer_size);
  165. dec_size = xan_unpack(s->scratch_buffer, s->buffer_size, src + offset,
  166. avpkt->size - offset - (src - avpkt->data));
  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) {
  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) {
  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, AVPacket *avpkt)
  215. {
  216. const uint8_t *buf = avpkt->data;
  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, size;
  221. int i, j;
  222. int ret;
  223. corr_off = AV_RL32(buf + 8);
  224. chroma_off = AV_RL32(buf + 4);
  225. if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
  226. return ret;
  227. size = avpkt->size - 4;
  228. if (corr_off >= avpkt->size) {
  229. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
  230. corr_off = 0;
  231. }
  232. if (corr_off)
  233. size = corr_off;
  234. if (chroma_off)
  235. size = FFMIN(size, chroma_off);
  236. ret = xan_unpack_luma(buf + 12, size, src, s->buffer_size >> 1);
  237. if (ret) {
  238. av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
  239. return ret;
  240. }
  241. ybuf = s->y_buffer;
  242. last = *src++;
  243. ybuf[0] = last << 1;
  244. for (j = 1; j < avctx->width - 1; j += 2) {
  245. cur = (last + *src++) & 0x1F;
  246. ybuf[j] = last + cur;
  247. ybuf[j+1] = cur << 1;
  248. last = cur;
  249. }
  250. ybuf[j] = last << 1;
  251. prev_buf = ybuf;
  252. ybuf += avctx->width;
  253. for (i = 1; i < avctx->height; i++) {
  254. last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
  255. ybuf[0] = last << 1;
  256. for (j = 1; j < avctx->width - 1; j += 2) {
  257. cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
  258. ybuf[j] = last + cur;
  259. ybuf[j+1] = cur << 1;
  260. last = cur;
  261. }
  262. ybuf[j] = last << 1;
  263. prev_buf = ybuf;
  264. ybuf += avctx->width;
  265. }
  266. if (corr_off) {
  267. int corr_end, dec_size;
  268. corr_end = avpkt->size;
  269. if (chroma_off > corr_off)
  270. corr_end = chroma_off;
  271. dec_size = xan_unpack(s->scratch_buffer, s->buffer_size,
  272. avpkt->data + 8 + corr_off,
  273. corr_end - corr_off);
  274. if (dec_size < 0)
  275. dec_size = 0;
  276. for (i = 0; i < dec_size; i++)
  277. s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
  278. }
  279. src = s->y_buffer;
  280. ybuf = s->pic.data[0];
  281. for (j = 0; j < avctx->height; j++) {
  282. for (i = 0; i < avctx->width; i++)
  283. ybuf[i] = (src[i] << 2) | (src[i] >> 3);
  284. src += avctx->width;
  285. ybuf += s->pic.linesize[0];
  286. }
  287. return 0;
  288. }
  289. static int xan_decode_frame_type1(AVCodecContext *avctx, AVPacket *avpkt)
  290. {
  291. const uint8_t *buf = avpkt->data;
  292. XanContext *s = avctx->priv_data;
  293. uint8_t *ybuf, *src = s->scratch_buffer;
  294. int cur, last;
  295. int i, j;
  296. int ret;
  297. if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
  298. return ret;
  299. ret = xan_unpack_luma(buf + 16, avpkt->size - 16, src,
  300. s->buffer_size >> 1);
  301. if (ret) {
  302. av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
  303. return ret;
  304. }
  305. ybuf = s->y_buffer;
  306. for (i = 0; i < avctx->height; i++) {
  307. last = (ybuf[0] + (*src++ << 1)) & 0x3F;
  308. ybuf[0] = last;
  309. for (j = 1; j < avctx->width - 1; j += 2) {
  310. cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
  311. ybuf[j] = (last + cur) >> 1;
  312. ybuf[j+1] = cur;
  313. last = cur;
  314. }
  315. ybuf[j] = last;
  316. ybuf += avctx->width;
  317. }
  318. src = s->y_buffer;
  319. ybuf = s->pic.data[0];
  320. for (j = 0; j < avctx->height; j++) {
  321. for (i = 0; i < avctx->width; i++)
  322. ybuf[i] = (src[i] << 2) | (src[i] >> 3);
  323. src += avctx->width;
  324. ybuf += s->pic.linesize[0];
  325. }
  326. return 0;
  327. }
  328. static int xan_decode_frame(AVCodecContext *avctx,
  329. void *data, int *data_size,
  330. AVPacket *avpkt)
  331. {
  332. XanContext *s = avctx->priv_data;
  333. int ftype;
  334. int ret;
  335. s->pic.reference = 1;
  336. s->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
  337. FF_BUFFER_HINTS_PRESERVE |
  338. FF_BUFFER_HINTS_REUSABLE;
  339. if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
  340. av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  341. return ret;
  342. }
  343. ftype = AV_RL32(avpkt->data);
  344. switch (ftype) {
  345. case 0:
  346. ret = xan_decode_frame_type0(avctx, avpkt);
  347. break;
  348. case 1:
  349. ret = xan_decode_frame_type1(avctx, avpkt);
  350. break;
  351. default:
  352. av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
  353. return -1;
  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. "xan_wc4",
  372. AVMEDIA_TYPE_VIDEO,
  373. CODEC_ID_XAN_WC4,
  374. sizeof(XanContext),
  375. xan_decode_init,
  376. NULL,
  377. xan_decode_end,
  378. xan_decode_frame,
  379. CODEC_CAP_DR1,
  380. .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
  381. };