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.

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