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
12KB

  1. /*
  2. * Wing Commander/Xan Video Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/xan.c
  23. * Xan video decoder for Wing Commander III computer game
  24. * by Mario Brito (mbrito@student.dei.uc.pt)
  25. * and Mike Melanson (melanson@pcisys.net)
  26. *
  27. * The xan_wc3 decoder outputs PAL8 data.
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include "libavutil/intreadwrite.h"
  34. #include "avcodec.h"
  35. #include "bytestream.h"
  36. #define ALT_BITSTREAM_READER_LE
  37. #include "get_bits.h"
  38. // for av_memcpy_backptr
  39. #include "libavutil/lzo.h"
  40. typedef struct XanContext {
  41. AVCodecContext *avctx;
  42. AVFrame last_frame;
  43. AVFrame current_frame;
  44. const unsigned char *buf;
  45. int size;
  46. /* scratch space */
  47. unsigned char *buffer1;
  48. int buffer1_size;
  49. unsigned char *buffer2;
  50. int buffer2_size;
  51. int frame_size;
  52. } XanContext;
  53. static av_cold int xan_decode_init(AVCodecContext *avctx)
  54. {
  55. XanContext *s = avctx->priv_data;
  56. s->avctx = avctx;
  57. s->frame_size = 0;
  58. if ((avctx->codec->id == CODEC_ID_XAN_WC3) &&
  59. (s->avctx->palctrl == NULL)) {
  60. av_log(avctx, AV_LOG_ERROR, " WC3 Xan video: palette expected.\n");
  61. return -1;
  62. }
  63. avctx->pix_fmt = PIX_FMT_PAL8;
  64. if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
  65. return -1;
  66. s->buffer1_size = avctx->width * avctx->height;
  67. s->buffer1 = av_malloc(s->buffer1_size);
  68. s->buffer2_size = avctx->width * avctx->height;
  69. s->buffer2 = av_malloc(s->buffer2_size + 130);
  70. if (!s->buffer1 || !s->buffer2)
  71. return -1;
  72. return 0;
  73. }
  74. static int xan_huffman_decode(unsigned char *dest, const unsigned char *src,
  75. int dest_len)
  76. {
  77. unsigned char byte = *src++;
  78. unsigned char ival = byte + 0x16;
  79. const unsigned char * ptr = src + byte*2;
  80. unsigned char val = ival;
  81. unsigned char *dest_end = dest + dest_len;
  82. GetBitContext gb;
  83. init_get_bits(&gb, ptr, 0); // FIXME: no src size available
  84. while ( val != 0x16 ) {
  85. val = src[val - 0x17 + get_bits1(&gb) * byte];
  86. if ( val < 0x16 ) {
  87. if (dest >= dest_end)
  88. return 0;
  89. *dest++ = val;
  90. val = ival;
  91. }
  92. }
  93. return 0;
  94. }
  95. /**
  96. * unpack simple compression
  97. *
  98. * @param dest destination buffer of dest_len, must be padded with at least 130 bytes
  99. */
  100. static void xan_unpack(unsigned char *dest, const unsigned char *src, int dest_len)
  101. {
  102. unsigned char opcode;
  103. int size;
  104. unsigned char *dest_end = dest + dest_len;
  105. while (dest < dest_end) {
  106. opcode = *src++;
  107. if (opcode < 0xe0) {
  108. int size2, back;
  109. if ( (opcode & 0x80) == 0 ) {
  110. size = opcode & 3;
  111. back = ((opcode & 0x60) << 3) + *src++ + 1;
  112. size2 = ((opcode & 0x1c) >> 2) + 3;
  113. } else if ( (opcode & 0x40) == 0 ) {
  114. size = *src >> 6;
  115. back = (bytestream_get_be16(&src) & 0x3fff) + 1;
  116. size2 = (opcode & 0x3f) + 4;
  117. } else {
  118. size = opcode & 3;
  119. back = ((opcode & 0x10) << 12) + bytestream_get_be16(&src) + 1;
  120. size2 = ((opcode & 0x0c) << 6) + *src++ + 5;
  121. if (size + size2 > dest_end - dest)
  122. return;
  123. }
  124. memcpy(dest, src, size); dest += size; src += size;
  125. av_memcpy_backptr(dest, back, size2);
  126. dest += size2;
  127. } else {
  128. int finish;
  129. size = ((opcode & 0x1f) << 2) + 4;
  130. finish = size > 0x70;
  131. if (finish)
  132. size = opcode & 3;
  133. memcpy(dest, src, size); dest += size; src += size;
  134. if (finish)
  135. return;
  136. }
  137. }
  138. }
  139. static inline void xan_wc3_output_pixel_run(XanContext *s,
  140. const unsigned char *pixel_buffer, int x, int y, int pixel_count)
  141. {
  142. int stride;
  143. int line_inc;
  144. int index;
  145. int current_x;
  146. int width = s->avctx->width;
  147. unsigned char *palette_plane;
  148. palette_plane = s->current_frame.data[0];
  149. stride = s->current_frame.linesize[0];
  150. line_inc = stride - width;
  151. index = y * stride + x;
  152. current_x = x;
  153. while((pixel_count--) && (index < s->frame_size)) {
  154. /* don't do a memcpy() here; keyframes generally copy an entire
  155. * frame of data and the stride needs to be accounted for */
  156. palette_plane[index++] = *pixel_buffer++;
  157. current_x++;
  158. if (current_x >= width) {
  159. index += line_inc;
  160. current_x = 0;
  161. }
  162. }
  163. }
  164. static inline void xan_wc3_copy_pixel_run(XanContext *s,
  165. int x, int y, int pixel_count, int motion_x, int motion_y)
  166. {
  167. int stride;
  168. int line_inc;
  169. int curframe_index, prevframe_index;
  170. int curframe_x, prevframe_x;
  171. int width = s->avctx->width;
  172. unsigned char *palette_plane, *prev_palette_plane;
  173. palette_plane = s->current_frame.data[0];
  174. prev_palette_plane = s->last_frame.data[0];
  175. stride = s->current_frame.linesize[0];
  176. line_inc = stride - width;
  177. curframe_index = y * stride + x;
  178. curframe_x = x;
  179. prevframe_index = (y + motion_y) * stride + x + motion_x;
  180. prevframe_x = x + motion_x;
  181. while((pixel_count--) && (curframe_index < s->frame_size)) {
  182. palette_plane[curframe_index++] =
  183. prev_palette_plane[prevframe_index++];
  184. curframe_x++;
  185. if (curframe_x >= width) {
  186. curframe_index += line_inc;
  187. curframe_x = 0;
  188. }
  189. prevframe_x++;
  190. if (prevframe_x >= width) {
  191. prevframe_index += line_inc;
  192. prevframe_x = 0;
  193. }
  194. }
  195. }
  196. static void xan_wc3_decode_frame(XanContext *s) {
  197. int width = s->avctx->width;
  198. int height = s->avctx->height;
  199. int total_pixels = width * height;
  200. unsigned char opcode;
  201. unsigned char flag = 0;
  202. int size = 0;
  203. int motion_x, motion_y;
  204. int x, y;
  205. unsigned char *opcode_buffer = s->buffer1;
  206. int opcode_buffer_size = s->buffer1_size;
  207. const unsigned char *imagedata_buffer = s->buffer2;
  208. /* pointers to segments inside the compressed chunk */
  209. const unsigned char *huffman_segment;
  210. const unsigned char *size_segment;
  211. const unsigned char *vector_segment;
  212. const unsigned char *imagedata_segment;
  213. huffman_segment = s->buf + AV_RL16(&s->buf[0]);
  214. size_segment = s->buf + AV_RL16(&s->buf[2]);
  215. vector_segment = s->buf + AV_RL16(&s->buf[4]);
  216. imagedata_segment = s->buf + AV_RL16(&s->buf[6]);
  217. xan_huffman_decode(opcode_buffer, huffman_segment, opcode_buffer_size);
  218. if (imagedata_segment[0] == 2)
  219. xan_unpack(s->buffer2, &imagedata_segment[1], s->buffer2_size);
  220. else
  221. imagedata_buffer = &imagedata_segment[1];
  222. /* use the decoded data segments to build the frame */
  223. x = y = 0;
  224. while (total_pixels) {
  225. opcode = *opcode_buffer++;
  226. size = 0;
  227. switch (opcode) {
  228. case 0:
  229. flag ^= 1;
  230. continue;
  231. case 1:
  232. case 2:
  233. case 3:
  234. case 4:
  235. case 5:
  236. case 6:
  237. case 7:
  238. case 8:
  239. size = opcode;
  240. break;
  241. case 12:
  242. case 13:
  243. case 14:
  244. case 15:
  245. case 16:
  246. case 17:
  247. case 18:
  248. size += (opcode - 10);
  249. break;
  250. case 9:
  251. case 19:
  252. size = *size_segment++;
  253. break;
  254. case 10:
  255. case 20:
  256. size = AV_RB16(&size_segment[0]);
  257. size_segment += 2;
  258. break;
  259. case 11:
  260. case 21:
  261. size = AV_RB24(size_segment);
  262. size_segment += 3;
  263. break;
  264. }
  265. if (opcode < 12) {
  266. flag ^= 1;
  267. if (flag) {
  268. /* run of (size) pixels is unchanged from last frame */
  269. xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
  270. } else {
  271. /* output a run of pixels from imagedata_buffer */
  272. xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);
  273. imagedata_buffer += size;
  274. }
  275. } else {
  276. /* run-based motion compensation from last frame */
  277. motion_x = sign_extend(*vector_segment >> 4, 4);
  278. motion_y = sign_extend(*vector_segment & 0xF, 4);
  279. vector_segment++;
  280. /* copy a run of pixels from the previous frame */
  281. xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
  282. flag = 0;
  283. }
  284. /* coordinate accounting */
  285. total_pixels -= size;
  286. y += (x + size) / width;
  287. x = (x + size) % width;
  288. }
  289. }
  290. static void xan_wc4_decode_frame(XanContext *s) {
  291. }
  292. static int xan_decode_frame(AVCodecContext *avctx,
  293. void *data, int *data_size,
  294. AVPacket *avpkt)
  295. {
  296. const uint8_t *buf = avpkt->data;
  297. int buf_size = avpkt->size;
  298. XanContext *s = avctx->priv_data;
  299. AVPaletteControl *palette_control = avctx->palctrl;
  300. if (avctx->get_buffer(avctx, &s->current_frame)) {
  301. av_log(s->avctx, AV_LOG_ERROR, " Xan Video: get_buffer() failed\n");
  302. return -1;
  303. }
  304. s->current_frame.reference = 3;
  305. if (!s->frame_size)
  306. s->frame_size = s->current_frame.linesize[0] * s->avctx->height;
  307. palette_control->palette_changed = 0;
  308. memcpy(s->current_frame.data[1], palette_control->palette,
  309. AVPALETTE_SIZE);
  310. s->current_frame.palette_has_changed = 1;
  311. s->buf = buf;
  312. s->size = buf_size;
  313. if (avctx->codec->id == CODEC_ID_XAN_WC3)
  314. xan_wc3_decode_frame(s);
  315. else if (avctx->codec->id == CODEC_ID_XAN_WC4)
  316. xan_wc4_decode_frame(s);
  317. /* release the last frame if it is allocated */
  318. if (s->last_frame.data[0])
  319. avctx->release_buffer(avctx, &s->last_frame);
  320. *data_size = sizeof(AVFrame);
  321. *(AVFrame*)data = s->current_frame;
  322. /* shuffle frames */
  323. FFSWAP(AVFrame, s->current_frame, s->last_frame);
  324. /* always report that the buffer was completely consumed */
  325. return buf_size;
  326. }
  327. static av_cold int xan_decode_end(AVCodecContext *avctx)
  328. {
  329. XanContext *s = avctx->priv_data;
  330. /* release the frames */
  331. if (s->last_frame.data[0])
  332. avctx->release_buffer(avctx, &s->last_frame);
  333. if (s->current_frame.data[0])
  334. avctx->release_buffer(avctx, &s->current_frame);
  335. av_free(s->buffer1);
  336. av_free(s->buffer2);
  337. return 0;
  338. }
  339. AVCodec xan_wc3_decoder = {
  340. "xan_wc3",
  341. CODEC_TYPE_VIDEO,
  342. CODEC_ID_XAN_WC3,
  343. sizeof(XanContext),
  344. xan_decode_init,
  345. NULL,
  346. xan_decode_end,
  347. xan_decode_frame,
  348. CODEC_CAP_DR1,
  349. .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III / Xan"),
  350. };
  351. /*
  352. AVCodec xan_wc4_decoder = {
  353. "xan_wc4",
  354. CODEC_TYPE_VIDEO,
  355. CODEC_ID_XAN_WC4,
  356. sizeof(XanContext),
  357. xan_decode_init,
  358. NULL,
  359. xan_decode_end,
  360. xan_decode_frame,
  361. CODEC_CAP_DR1,
  362. };
  363. */