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.

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