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.

436 lines
12KB

  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "bytestream.h"
  25. #define ALT_BITSTREAM_READER_LE
  26. #include "get_bits.h"
  27. // for av_memcpy_backptr
  28. #include "libavutil/lzo.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. } 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 = PIX_FMT_YUV420P;
  41. s->buffer_size = avctx->width * avctx->height;
  42. s->y_buffer = av_malloc(s->buffer_size);
  43. if (!s->y_buffer)
  44. return AVERROR(ENOMEM);
  45. s->scratch_buffer = av_malloc(s->buffer_size + 130);
  46. if (!s->scratch_buffer) {
  47. av_freep(&s->y_buffer);
  48. return AVERROR(ENOMEM);
  49. }
  50. return 0;
  51. }
  52. static int xan_unpack_luma(const uint8_t *src, const int src_size,
  53. uint8_t *dst, const int dst_size)
  54. {
  55. int tree_size, eof;
  56. const uint8_t *tree;
  57. int bits, mask;
  58. int tree_root, node;
  59. const uint8_t *dst_end = dst + dst_size;
  60. const uint8_t *src_end = src + src_size;
  61. tree_size = *src++;
  62. eof = *src++;
  63. tree = src - eof * 2 - 2;
  64. tree_root = eof + tree_size;
  65. src += tree_size * 2;
  66. node = tree_root;
  67. bits = *src++;
  68. mask = 0x80;
  69. for (;;) {
  70. int bit = !!(bits & mask);
  71. mask >>= 1;
  72. node = tree[node*2 + bit];
  73. if (node == eof)
  74. break;
  75. if (node < eof) {
  76. *dst++ = node;
  77. if (dst > dst_end)
  78. break;
  79. node = tree_root;
  80. }
  81. if (!mask) {
  82. bits = *src++;
  83. if (src > src_end)
  84. break;
  85. mask = 0x80;
  86. }
  87. }
  88. return dst != dst_end;
  89. }
  90. /* almost the same as in xan_wc3 decoder */
  91. static int xan_unpack(uint8_t *dest, const int dest_len,
  92. const uint8_t *src, const int src_len)
  93. {
  94. uint8_t opcode;
  95. int size;
  96. uint8_t *orig_dest = dest;
  97. const uint8_t *src_end = src + src_len;
  98. const uint8_t *dest_end = dest + dest_len;
  99. while (dest < dest_end) {
  100. opcode = *src++;
  101. if (opcode < 0xe0) {
  102. int size2, back;
  103. if ((opcode & 0x80) == 0) {
  104. size = opcode & 3;
  105. back = ((opcode & 0x60) << 3) + *src++ + 1;
  106. size2 = ((opcode & 0x1c) >> 2) + 3;
  107. } else if ((opcode & 0x40) == 0) {
  108. size = *src >> 6;
  109. back = (bytestream_get_be16(&src) & 0x3fff) + 1;
  110. size2 = (opcode & 0x3f) + 4;
  111. } else {
  112. size = opcode & 3;
  113. back = ((opcode & 0x10) << 12) + bytestream_get_be16(&src) + 1;
  114. size2 = ((opcode & 0x0c) << 6) + *src++ + 5;
  115. if (size + size2 > dest_end - dest)
  116. break;
  117. }
  118. if (src + size > src_end ||
  119. dest + size + size2 > dest_end ||
  120. dest + size - orig_dest < back )
  121. return -1;
  122. bytestream_get_buffer(&src, dest, size);
  123. dest += size;
  124. av_memcpy_backptr(dest, back, size2);
  125. dest += size2;
  126. } else {
  127. int finish = opcode >= 0xfc;
  128. size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
  129. if (src + size > src_end || dest + size > dest_end)
  130. return -1;
  131. bytestream_get_buffer(&src, dest, size);
  132. dest += size;
  133. if (finish)
  134. break;
  135. }
  136. }
  137. return dest - orig_dest;
  138. }
  139. static int xan_decode_chroma(AVCodecContext *avctx, AVPacket *avpkt)
  140. {
  141. const uint8_t *buf = avpkt->data;
  142. XanContext *s = avctx->priv_data;
  143. uint8_t *U, *V;
  144. unsigned chroma_off;
  145. int val, uval, vval;
  146. int i, j;
  147. const uint8_t *src, *src_end;
  148. const uint8_t *table;
  149. int mode, offset, dec_size;
  150. chroma_off = AV_RL32(buf + 4);
  151. if (!chroma_off)
  152. return 0;
  153. if (chroma_off + 10 >= avpkt->size) {
  154. av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
  155. return -1;
  156. }
  157. src = avpkt->data + 4 + chroma_off;
  158. table = src + 2;
  159. mode = bytestream_get_le16(&src);
  160. offset = bytestream_get_le16(&src) * 2;
  161. if (src - avpkt->data >= avpkt->size - offset) {
  162. av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
  163. return -1;
  164. }
  165. memset(s->scratch_buffer, 0, s->buffer_size);
  166. dec_size = xan_unpack(s->scratch_buffer, s->buffer_size, src + offset,
  167. avpkt->size - offset - (src - avpkt->data));
  168. if (dec_size < 0) {
  169. av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
  170. return -1;
  171. }
  172. U = s->pic.data[1];
  173. V = s->pic.data[2];
  174. src = s->scratch_buffer;
  175. src_end = src + dec_size;
  176. if (mode) {
  177. for (j = 0; j < avctx->height >> 1; j++) {
  178. for (i = 0; i < avctx->width >> 1; i++) {
  179. if (src_end - src < 1)
  180. return 0;
  181. val = *src++;
  182. if (val) {
  183. val = AV_RL16(table + (val << 1));
  184. uval = (val >> 3) & 0xF8;
  185. vval = (val >> 8) & 0xF8;
  186. U[i] = uval | (uval >> 5);
  187. V[i] = vval | (vval >> 5);
  188. }
  189. }
  190. U += s->pic.linesize[1];
  191. V += s->pic.linesize[2];
  192. }
  193. } else {
  194. uint8_t *U2 = U + s->pic.linesize[1];
  195. uint8_t *V2 = V + s->pic.linesize[2];
  196. for (j = 0; j < avctx->height >> 2; j++) {
  197. for (i = 0; i < avctx->width >> 1; i += 2) {
  198. if (src_end - src < 1)
  199. return 0;
  200. val = *src++;
  201. if (val) {
  202. val = AV_RL16(table + (val << 1));
  203. uval = (val >> 3) & 0xF8;
  204. vval = (val >> 8) & 0xF8;
  205. U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
  206. V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
  207. }
  208. }
  209. U += s->pic.linesize[1] * 2;
  210. V += s->pic.linesize[2] * 2;
  211. U2 += s->pic.linesize[1] * 2;
  212. V2 += s->pic.linesize[2] * 2;
  213. }
  214. }
  215. return 0;
  216. }
  217. static int xan_decode_frame_type0(AVCodecContext *avctx, AVPacket *avpkt)
  218. {
  219. const uint8_t *buf = avpkt->data;
  220. XanContext *s = avctx->priv_data;
  221. uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
  222. unsigned chroma_off, corr_off;
  223. int cur, last, size;
  224. int i, j;
  225. int ret;
  226. corr_off = AV_RL32(buf + 8);
  227. chroma_off = AV_RL32(buf + 4);
  228. if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
  229. return ret;
  230. size = avpkt->size - 4;
  231. if (corr_off >= avpkt->size) {
  232. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
  233. corr_off = 0;
  234. }
  235. if (corr_off)
  236. size = corr_off;
  237. if (chroma_off)
  238. size = FFMIN(size, chroma_off);
  239. ret = xan_unpack_luma(buf + 12, size, src, s->buffer_size >> 1);
  240. if (ret) {
  241. av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
  242. return ret;
  243. }
  244. ybuf = s->y_buffer;
  245. last = *src++;
  246. ybuf[0] = last << 1;
  247. for (j = 1; j < avctx->width - 1; j += 2) {
  248. cur = (last + *src++) & 0x1F;
  249. ybuf[j] = last + cur;
  250. ybuf[j+1] = cur << 1;
  251. last = cur;
  252. }
  253. ybuf[j] = last << 1;
  254. prev_buf = ybuf;
  255. ybuf += avctx->width;
  256. for (i = 1; i < avctx->height; i++) {
  257. last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
  258. ybuf[0] = last << 1;
  259. for (j = 1; j < avctx->width - 1; j += 2) {
  260. cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
  261. ybuf[j] = last + cur;
  262. ybuf[j+1] = cur << 1;
  263. last = cur;
  264. }
  265. ybuf[j] = last << 1;
  266. prev_buf = ybuf;
  267. ybuf += avctx->width;
  268. }
  269. if (corr_off) {
  270. int corr_end, dec_size;
  271. corr_end = avpkt->size;
  272. if (chroma_off > corr_off)
  273. corr_end = chroma_off;
  274. dec_size = xan_unpack(s->scratch_buffer, s->buffer_size,
  275. avpkt->data + 8 + corr_off,
  276. corr_end - corr_off);
  277. if (dec_size < 0)
  278. dec_size = 0;
  279. else
  280. dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
  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, AVPacket *avpkt)
  295. {
  296. const uint8_t *buf = avpkt->data;
  297. XanContext *s = avctx->priv_data;
  298. uint8_t *ybuf, *src = s->scratch_buffer;
  299. int cur, last;
  300. int i, j;
  301. int ret;
  302. if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
  303. return ret;
  304. ret = xan_unpack_luma(buf + 16, avpkt->size - 16, 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 *data_size,
  335. AVPacket *avpkt)
  336. {
  337. XanContext *s = avctx->priv_data;
  338. int ftype;
  339. int ret;
  340. s->pic.reference = 1;
  341. s->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
  342. FF_BUFFER_HINTS_PRESERVE |
  343. FF_BUFFER_HINTS_REUSABLE;
  344. if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
  345. av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  346. return ret;
  347. }
  348. ftype = AV_RL32(avpkt->data);
  349. switch (ftype) {
  350. case 0:
  351. ret = xan_decode_frame_type0(avctx, avpkt);
  352. break;
  353. case 1:
  354. ret = xan_decode_frame_type1(avctx, avpkt);
  355. break;
  356. default:
  357. av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
  358. return -1;
  359. }
  360. if (ret)
  361. return ret;
  362. *data_size = sizeof(AVFrame);
  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 = 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. };