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.

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