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.

432 lines
13KB

  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 "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. if (src_end - src < 1)
  180. return 0;
  181. val = *src++;
  182. if (val) {
  183. if (val >= table_size)
  184. return AVERROR_INVALIDDATA;
  185. val = AV_RL16(table + (val << 1));
  186. uval = (val >> 3) & 0xF8;
  187. vval = (val >> 8) & 0xF8;
  188. U[i] = uval | (uval >> 5);
  189. V[i] = vval | (vval >> 5);
  190. }
  191. }
  192. U += s->pic.linesize[1];
  193. V += s->pic.linesize[2];
  194. }
  195. } else {
  196. uint8_t *U2 = U + s->pic.linesize[1];
  197. uint8_t *V2 = V + s->pic.linesize[2];
  198. for (j = 0; j < avctx->height >> 2; j++) {
  199. for (i = 0; i < avctx->width >> 1; i += 2) {
  200. if (src_end - src < 1)
  201. return 0;
  202. val = *src++;
  203. if (val) {
  204. if (val >= table_size)
  205. return AVERROR_INVALIDDATA;
  206. val = AV_RL16(table + (val << 1));
  207. uval = (val >> 3) & 0xF8;
  208. vval = (val >> 8) & 0xF8;
  209. U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
  210. V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
  211. }
  212. }
  213. U += s->pic.linesize[1] * 2;
  214. V += s->pic.linesize[2] * 2;
  215. U2 += s->pic.linesize[1] * 2;
  216. V2 += s->pic.linesize[2] * 2;
  217. }
  218. }
  219. return 0;
  220. }
  221. static int xan_decode_frame_type0(AVCodecContext *avctx)
  222. {
  223. XanContext *s = avctx->priv_data;
  224. uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
  225. unsigned chroma_off, corr_off;
  226. int cur, last;
  227. int i, j;
  228. int ret;
  229. chroma_off = bytestream2_get_le32(&s->gb);
  230. corr_off = bytestream2_get_le32(&s->gb);
  231. if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
  232. return ret;
  233. if (corr_off >= bytestream2_size(&s->gb)) {
  234. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
  235. corr_off = 0;
  236. }
  237. bytestream2_seek(&s->gb, 12, SEEK_SET);
  238. ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
  239. if (ret) {
  240. av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
  241. return ret;
  242. }
  243. ybuf = s->y_buffer;
  244. last = *src++;
  245. ybuf[0] = last << 1;
  246. for (j = 1; j < avctx->width - 1; j += 2) {
  247. cur = (last + *src++) & 0x1F;
  248. ybuf[j] = last + cur;
  249. ybuf[j+1] = cur << 1;
  250. last = cur;
  251. }
  252. ybuf[j] = last << 1;
  253. prev_buf = ybuf;
  254. ybuf += avctx->width;
  255. for (i = 1; i < avctx->height; i++) {
  256. last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
  257. ybuf[0] = last << 1;
  258. for (j = 1; j < avctx->width - 1; j += 2) {
  259. cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
  260. ybuf[j] = last + cur;
  261. ybuf[j+1] = cur << 1;
  262. last = cur;
  263. }
  264. ybuf[j] = last << 1;
  265. prev_buf = ybuf;
  266. ybuf += avctx->width;
  267. }
  268. if (corr_off) {
  269. int dec_size;
  270. bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
  271. dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
  272. if (dec_size < 0)
  273. dec_size = 0;
  274. else
  275. dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
  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)
  290. {
  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, bytestream2_get_le32(&s->gb))) != 0)
  297. return ret;
  298. bytestream2_seek(&s->gb, 16, SEEK_SET);
  299. ret = xan_unpack_luma(s, 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 = 3;
  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. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  344. ftype = bytestream2_get_le32(&s->gb);
  345. switch (ftype) {
  346. case 0:
  347. ret = xan_decode_frame_type0(avctx);
  348. break;
  349. case 1:
  350. ret = xan_decode_frame_type1(avctx);
  351. break;
  352. default:
  353. av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
  354. return -1;
  355. }
  356. if (ret)
  357. return ret;
  358. *data_size = sizeof(AVFrame);
  359. *(AVFrame*)data = s->pic;
  360. return avpkt->size;
  361. }
  362. static av_cold int xan_decode_end(AVCodecContext *avctx)
  363. {
  364. XanContext *s = avctx->priv_data;
  365. if (s->pic.data[0])
  366. avctx->release_buffer(avctx, &s->pic);
  367. av_freep(&s->y_buffer);
  368. av_freep(&s->scratch_buffer);
  369. return 0;
  370. }
  371. AVCodec ff_xan_wc4_decoder = {
  372. .name = "xan_wc4",
  373. .type = AVMEDIA_TYPE_VIDEO,
  374. .id = AV_CODEC_ID_XAN_WC4,
  375. .priv_data_size = sizeof(XanContext),
  376. .init = xan_decode_init,
  377. .close = xan_decode_end,
  378. .decode = xan_decode_frame,
  379. .capabilities = CODEC_CAP_DR1,
  380. .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
  381. };