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.

441 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 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 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 table_size;
  150. int mode, offset, dec_size;
  151. chroma_off = AV_RL32(buf + 4);
  152. if (!chroma_off)
  153. return 0;
  154. if (chroma_off + 10 >= avpkt->size) {
  155. av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
  156. return -1;
  157. }
  158. src = avpkt->data + 4 + chroma_off;
  159. table = src + 2;
  160. table_size = avpkt->data + avpkt->size - table;
  161. mode = bytestream_get_le16(&src);
  162. offset = bytestream_get_le16(&src) * 2;
  163. if (src - avpkt->data >= avpkt->size - offset) {
  164. av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
  165. return -1;
  166. }
  167. memset(s->scratch_buffer, 0, s->buffer_size);
  168. dec_size = xan_unpack(s->scratch_buffer, s->buffer_size, src + offset,
  169. avpkt->size - offset - (src - avpkt->data));
  170. if (dec_size < 0) {
  171. av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
  172. return -1;
  173. }
  174. U = s->pic.data[1];
  175. V = s->pic.data[2];
  176. src = s->scratch_buffer;
  177. src_end = src + dec_size;
  178. if (mode) {
  179. for (j = 0; j < avctx->height >> 1; j++) {
  180. for (i = 0; i < avctx->width >> 1; i++) {
  181. if (src_end - src < 1)
  182. return 0;
  183. val = *src++;
  184. if (val) {
  185. if (val << 1 >= table_size)
  186. return AVERROR_INVALIDDATA;
  187. val = AV_RL16(table + (val << 1));
  188. uval = (val >> 3) & 0xF8;
  189. vval = (val >> 8) & 0xF8;
  190. U[i] = uval | (uval >> 5);
  191. V[i] = vval | (vval >> 5);
  192. }
  193. }
  194. U += s->pic.linesize[1];
  195. V += s->pic.linesize[2];
  196. }
  197. } else {
  198. uint8_t *U2 = U + s->pic.linesize[1];
  199. uint8_t *V2 = V + s->pic.linesize[2];
  200. for (j = 0; j < avctx->height >> 2; j++) {
  201. for (i = 0; i < avctx->width >> 1; i += 2) {
  202. if (src_end - src < 1)
  203. return 0;
  204. val = *src++;
  205. if (val) {
  206. if (val << 1 >= table_size)
  207. return AVERROR_INVALIDDATA;
  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. }
  221. return 0;
  222. }
  223. static int xan_decode_frame_type0(AVCodecContext *avctx, AVPacket *avpkt)
  224. {
  225. const uint8_t *buf = avpkt->data;
  226. XanContext *s = avctx->priv_data;
  227. uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
  228. unsigned chroma_off, corr_off;
  229. int cur, last, size;
  230. int i, j;
  231. int ret;
  232. corr_off = AV_RL32(buf + 8);
  233. chroma_off = AV_RL32(buf + 4);
  234. if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
  235. return ret;
  236. size = avpkt->size - 4;
  237. if (corr_off >= avpkt->size) {
  238. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
  239. corr_off = 0;
  240. }
  241. if (corr_off)
  242. size = corr_off;
  243. if (chroma_off)
  244. size = FFMIN(size, chroma_off);
  245. ret = xan_unpack_luma(buf + 12, size, 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 corr_end, dec_size;
  277. corr_end = avpkt->size;
  278. if (chroma_off > corr_off)
  279. corr_end = chroma_off;
  280. dec_size = xan_unpack(s->scratch_buffer, s->buffer_size,
  281. avpkt->data + 8 + corr_off,
  282. corr_end - corr_off);
  283. if (dec_size < 0)
  284. dec_size = 0;
  285. else
  286. dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
  287. for (i = 0; i < dec_size; i++)
  288. s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
  289. }
  290. src = s->y_buffer;
  291. ybuf = s->pic.data[0];
  292. for (j = 0; j < avctx->height; j++) {
  293. for (i = 0; i < avctx->width; i++)
  294. ybuf[i] = (src[i] << 2) | (src[i] >> 3);
  295. src += avctx->width;
  296. ybuf += s->pic.linesize[0];
  297. }
  298. return 0;
  299. }
  300. static int xan_decode_frame_type1(AVCodecContext *avctx, AVPacket *avpkt)
  301. {
  302. const uint8_t *buf = avpkt->data;
  303. XanContext *s = avctx->priv_data;
  304. uint8_t *ybuf, *src = s->scratch_buffer;
  305. int cur, last;
  306. int i, j;
  307. int ret;
  308. if ((ret = xan_decode_chroma(avctx, avpkt)) != 0)
  309. return ret;
  310. ret = xan_unpack_luma(buf + 16, avpkt->size - 16, src,
  311. s->buffer_size >> 1);
  312. if (ret) {
  313. av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
  314. return ret;
  315. }
  316. ybuf = s->y_buffer;
  317. for (i = 0; i < avctx->height; i++) {
  318. last = (ybuf[0] + (*src++ << 1)) & 0x3F;
  319. ybuf[0] = last;
  320. for (j = 1; j < avctx->width - 1; j += 2) {
  321. cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
  322. ybuf[j] = (last + cur) >> 1;
  323. ybuf[j+1] = cur;
  324. last = cur;
  325. }
  326. ybuf[j] = last;
  327. ybuf += avctx->width;
  328. }
  329. src = s->y_buffer;
  330. ybuf = s->pic.data[0];
  331. for (j = 0; j < avctx->height; j++) {
  332. for (i = 0; i < avctx->width; i++)
  333. ybuf[i] = (src[i] << 2) | (src[i] >> 3);
  334. src += avctx->width;
  335. ybuf += s->pic.linesize[0];
  336. }
  337. return 0;
  338. }
  339. static int xan_decode_frame(AVCodecContext *avctx,
  340. void *data, int *data_size,
  341. AVPacket *avpkt)
  342. {
  343. XanContext *s = avctx->priv_data;
  344. int ftype;
  345. int ret;
  346. s->pic.reference = 3;
  347. s->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
  348. FF_BUFFER_HINTS_PRESERVE |
  349. FF_BUFFER_HINTS_REUSABLE;
  350. if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
  351. av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  352. return ret;
  353. }
  354. ftype = AV_RL32(avpkt->data);
  355. switch (ftype) {
  356. case 0:
  357. ret = xan_decode_frame_type0(avctx, avpkt);
  358. break;
  359. case 1:
  360. ret = xan_decode_frame_type1(avctx, avpkt);
  361. break;
  362. default:
  363. av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
  364. return -1;
  365. }
  366. if (ret)
  367. return ret;
  368. *data_size = sizeof(AVFrame);
  369. *(AVFrame*)data = s->pic;
  370. return avpkt->size;
  371. }
  372. static av_cold int xan_decode_end(AVCodecContext *avctx)
  373. {
  374. XanContext *s = avctx->priv_data;
  375. if (s->pic.data[0])
  376. avctx->release_buffer(avctx, &s->pic);
  377. av_freep(&s->y_buffer);
  378. av_freep(&s->scratch_buffer);
  379. return 0;
  380. }
  381. AVCodec ff_xan_wc4_decoder = {
  382. .name = "xan_wc4",
  383. .type = AVMEDIA_TYPE_VIDEO,
  384. .id = CODEC_ID_XAN_WC4,
  385. .priv_data_size = sizeof(XanContext),
  386. .init = xan_decode_init,
  387. .close = xan_decode_end,
  388. .decode = xan_decode_frame,
  389. .capabilities = CODEC_CAP_DR1,
  390. .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
  391. };