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.

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