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.

430 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. return -1;
  120. bytestream_get_buffer(&src, dest, size);
  121. dest += size;
  122. av_memcpy_backptr(dest, back, size2);
  123. dest += size2;
  124. } else {
  125. int finish = opcode >= 0xfc;
  126. size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
  127. if (src + size > src_end || dest + size > dest_end)
  128. return -1;
  129. bytestream_get_buffer(&src, dest, size);
  130. dest += size;
  131. if (finish)
  132. break;
  133. }
  134. }
  135. return dest - orig_dest;
  136. }
  137. static int xan_decode_chroma(AVCodecContext *avctx, AVPacket *avpkt)
  138. {
  139. const uint8_t *buf = avpkt->data;
  140. XanContext *s = avctx->priv_data;
  141. uint8_t *U, *V;
  142. unsigned chroma_off;
  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;
  148. chroma_off = AV_RL32(buf + 4);
  149. if (!chroma_off)
  150. return 0;
  151. if (chroma_off + 10 >= avpkt->size) {
  152. av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
  153. return -1;
  154. }
  155. src = avpkt->data + 4 + chroma_off;
  156. table = src + 2;
  157. mode = bytestream_get_le16(&src);
  158. offset = bytestream_get_le16(&src) * 2;
  159. if (src - avpkt->data >= avpkt->size - offset) {
  160. av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
  161. return -1;
  162. }
  163. memset(s->scratch_buffer, 0, s->buffer_size);
  164. dec_size = xan_unpack(s->scratch_buffer, s->buffer_size, src + offset,
  165. avpkt->size - offset - (src - avpkt->data));
  166. if (dec_size < 0) {
  167. av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
  168. return -1;
  169. }
  170. U = s->pic.data[1];
  171. V = s->pic.data[2];
  172. src = s->scratch_buffer;
  173. src_end = src + dec_size;
  174. if (mode) {
  175. for (j = 0; j < avctx->height >> 1; j++) {
  176. for (i = 0; i < avctx->width >> 1; i++) {
  177. val = *src++;
  178. if (val) {
  179. val = AV_RL16(table + (val << 1));
  180. uval = (val >> 3) & 0xF8;
  181. vval = (val >> 8) & 0xF8;
  182. U[i] = uval | (uval >> 5);
  183. V[i] = vval | (vval >> 5);
  184. }
  185. if (src == src_end)
  186. return 0;
  187. }
  188. U += s->pic.linesize[1];
  189. V += s->pic.linesize[2];
  190. }
  191. } else {
  192. uint8_t *U2 = U + s->pic.linesize[1];
  193. uint8_t *V2 = V + s->pic.linesize[2];
  194. for (j = 0; j < avctx->height >> 2; j++) {
  195. for (i = 0; i < avctx->width >> 1; i += 2) {
  196. val = *src++;
  197. if (val) {
  198. val = AV_RL16(table + (val << 1));
  199. uval = (val >> 3) & 0xF8;
  200. vval = (val >> 8) & 0xF8;
  201. U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
  202. V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
  203. }
  204. }
  205. U += s->pic.linesize[1] * 2;
  206. V += s->pic.linesize[2] * 2;
  207. U2 += s->pic.linesize[1] * 2;
  208. V2 += s->pic.linesize[2] * 2;
  209. }
  210. }
  211. return 0;
  212. }
  213. static int xan_decode_frame_type0(AVCodecContext *avctx, AVPacket *avpkt)
  214. {
  215. const uint8_t *buf = avpkt->data;
  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, size;
  220. int i, j;
  221. int ret;
  222. corr_off = AV_RL32(buf + 8);
  223. chroma_off = AV_RL32(buf + 4);
  224. if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
  225. return ret;
  226. size = avpkt->size - 4;
  227. if (corr_off >= avpkt->size) {
  228. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
  229. corr_off = 0;
  230. }
  231. if (corr_off)
  232. size = corr_off;
  233. if (chroma_off)
  234. size = FFMIN(size, chroma_off);
  235. ret = xan_unpack_luma(buf + 12, size, src, s->buffer_size >> 1);
  236. if (ret) {
  237. av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
  238. return ret;
  239. }
  240. ybuf = s->y_buffer;
  241. last = *src++;
  242. ybuf[0] = last << 1;
  243. for (j = 1; j < avctx->width - 1; j += 2) {
  244. cur = (last + *src++) & 0x1F;
  245. ybuf[j] = last + cur;
  246. ybuf[j+1] = cur << 1;
  247. last = cur;
  248. }
  249. ybuf[j] = last << 1;
  250. prev_buf = ybuf;
  251. ybuf += avctx->width;
  252. for (i = 1; i < avctx->height; i++) {
  253. last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
  254. ybuf[0] = last << 1;
  255. for (j = 1; j < avctx->width - 1; j += 2) {
  256. cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
  257. ybuf[j] = last + cur;
  258. ybuf[j+1] = cur << 1;
  259. last = cur;
  260. }
  261. ybuf[j] = last << 1;
  262. prev_buf = ybuf;
  263. ybuf += avctx->width;
  264. }
  265. if (corr_off) {
  266. int corr_end, dec_size;
  267. corr_end = avpkt->size;
  268. if (chroma_off > corr_off)
  269. corr_end = chroma_off;
  270. dec_size = xan_unpack(s->scratch_buffer, s->buffer_size,
  271. avpkt->data + 8 + corr_off,
  272. corr_end - corr_off);
  273. if (dec_size < 0)
  274. dec_size = 0;
  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, AVPacket *avpkt)
  289. {
  290. const uint8_t *buf = avpkt->data;
  291. XanContext *s = avctx->priv_data;
  292. uint8_t *ybuf, *src = s->scratch_buffer;
  293. int cur, last;
  294. int i, j;
  295. int ret;
  296. if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
  297. return ret;
  298. ret = xan_unpack_luma(buf + 16, avpkt->size - 16, 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 = 1;
  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. ftype = AV_RL32(avpkt->data);
  343. switch (ftype) {
  344. case 0:
  345. ret = xan_decode_frame_type0(avctx, avpkt);
  346. break;
  347. case 1:
  348. ret = xan_decode_frame_type1(avctx, avpkt);
  349. break;
  350. default:
  351. av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
  352. return -1;
  353. }
  354. if (ret)
  355. return ret;
  356. *data_size = sizeof(AVFrame);
  357. *(AVFrame*)data = s->pic;
  358. return avpkt->size;
  359. }
  360. static av_cold int xan_decode_end(AVCodecContext *avctx)
  361. {
  362. XanContext *s = avctx->priv_data;
  363. if (s->pic.data[0])
  364. avctx->release_buffer(avctx, &s->pic);
  365. av_freep(&s->y_buffer);
  366. av_freep(&s->scratch_buffer);
  367. return 0;
  368. }
  369. AVCodec ff_xan_wc4_decoder = {
  370. "xan_wc4",
  371. AVMEDIA_TYPE_VIDEO,
  372. CODEC_ID_XAN_WC4,
  373. sizeof(XanContext),
  374. xan_decode_init,
  375. NULL,
  376. xan_decode_end,
  377. xan_decode_frame,
  378. CODEC_CAP_DR1,
  379. .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
  380. };