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.

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