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.

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