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.

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