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.

447 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
  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 "libavutil/intreadwrite.h"
  33. #include "avcodec.h"
  34. #include "bytestream.h"
  35. #define ALT_BITSTREAM_READER_LE
  36. #include "get_bits.h"
  37. // for av_memcpy_backptr
  38. #include "libavutil/lzo.h"
  39. typedef struct XanContext {
  40. AVCodecContext *avctx;
  41. AVFrame last_frame;
  42. AVFrame current_frame;
  43. const unsigned char *buf;
  44. int size;
  45. /* scratch space */
  46. unsigned char *buffer1;
  47. int buffer1_size;
  48. unsigned char *buffer2;
  49. int buffer2_size;
  50. int frame_size;
  51. } XanContext;
  52. static av_cold int xan_decode_init(AVCodecContext *avctx)
  53. {
  54. XanContext *s = avctx->priv_data;
  55. s->avctx = avctx;
  56. s->frame_size = 0;
  57. if ((avctx->codec->id == CODEC_ID_XAN_WC3) &&
  58. (s->avctx->palctrl == NULL)) {
  59. av_log(avctx, AV_LOG_ERROR, " WC3 Xan video: palette expected.\n");
  60. return -1;
  61. }
  62. avctx->pix_fmt = PIX_FMT_PAL8;
  63. s->buffer1_size = avctx->width * avctx->height;
  64. s->buffer1 = av_malloc(s->buffer1_size);
  65. if (!s->buffer1)
  66. return -1;
  67. s->buffer2_size = avctx->width * avctx->height;
  68. s->buffer2 = av_malloc(s->buffer2_size + 130);
  69. if (!s->buffer2) {
  70. av_freep(&s->buffer1);
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. static int xan_huffman_decode(unsigned char *dest, int dest_len,
  76. const unsigned char *src, int src_len)
  77. {
  78. unsigned char byte = *src++;
  79. unsigned char ival = byte + 0x16;
  80. const unsigned char * ptr = src + byte*2;
  81. int ptr_len = src_len - 1 - byte*2;
  82. unsigned char val = ival;
  83. unsigned char *dest_end = dest + dest_len;
  84. GetBitContext gb;
  85. init_get_bits(&gb, ptr, ptr_len * 8);
  86. while ( val != 0x16 ) {
  87. val = src[val - 0x17 + get_bits1(&gb) * byte];
  88. if ( val < 0x16 ) {
  89. if (dest >= dest_end)
  90. return 0;
  91. *dest++ = val;
  92. val = ival;
  93. }
  94. }
  95. return 0;
  96. }
  97. /**
  98. * unpack simple compression
  99. *
  100. * @param dest destination buffer of dest_len, must be padded with at least 130 bytes
  101. */
  102. static void xan_unpack(unsigned char *dest, const unsigned char *src, int dest_len)
  103. {
  104. unsigned char opcode;
  105. int size;
  106. unsigned char *dest_end = dest + dest_len;
  107. while (dest < dest_end) {
  108. opcode = *src++;
  109. if (opcode < 0xe0) {
  110. int size2, back;
  111. if ( (opcode & 0x80) == 0 ) {
  112. size = opcode & 3;
  113. back = ((opcode & 0x60) << 3) + *src++ + 1;
  114. size2 = ((opcode & 0x1c) >> 2) + 3;
  115. } else if ( (opcode & 0x40) == 0 ) {
  116. size = *src >> 6;
  117. back = (bytestream_get_be16(&src) & 0x3fff) + 1;
  118. size2 = (opcode & 0x3f) + 4;
  119. } else {
  120. size = opcode & 3;
  121. back = ((opcode & 0x10) << 12) + bytestream_get_be16(&src) + 1;
  122. size2 = ((opcode & 0x0c) << 6) + *src++ + 5;
  123. if (size + size2 > dest_end - dest)
  124. return;
  125. }
  126. memcpy(dest, src, size); dest += size; src += size;
  127. av_memcpy_backptr(dest, back, size2);
  128. dest += size2;
  129. } else {
  130. int finish = opcode >= 0xfc;
  131. size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
  132. memcpy(dest, src, size); dest += size; src += size;
  133. if (finish)
  134. return;
  135. }
  136. }
  137. }
  138. static inline void xan_wc3_output_pixel_run(XanContext *s,
  139. const unsigned char *pixel_buffer, int x, int y, int pixel_count)
  140. {
  141. int stride;
  142. int line_inc;
  143. int index;
  144. int current_x;
  145. int width = s->avctx->width;
  146. unsigned char *palette_plane;
  147. palette_plane = s->current_frame.data[0];
  148. stride = s->current_frame.linesize[0];
  149. line_inc = stride - width;
  150. index = y * stride + x;
  151. current_x = x;
  152. while(pixel_count && (index < s->frame_size)) {
  153. int count = FFMIN(pixel_count, width - current_x);
  154. memcpy(palette_plane + index, pixel_buffer, count);
  155. pixel_count -= count;
  156. index += count;
  157. pixel_buffer += count;
  158. current_x += count;
  159. if (current_x >= width) {
  160. index += line_inc;
  161. current_x = 0;
  162. }
  163. }
  164. }
  165. static inline void xan_wc3_copy_pixel_run(XanContext *s,
  166. int x, int y, int pixel_count, int motion_x, int motion_y)
  167. {
  168. int stride;
  169. int line_inc;
  170. int curframe_index, prevframe_index;
  171. int curframe_x, prevframe_x;
  172. int width = s->avctx->width;
  173. unsigned char *palette_plane, *prev_palette_plane;
  174. palette_plane = s->current_frame.data[0];
  175. prev_palette_plane = s->last_frame.data[0];
  176. stride = s->current_frame.linesize[0];
  177. line_inc = stride - width;
  178. curframe_index = y * stride + x;
  179. curframe_x = x;
  180. prevframe_index = (y + motion_y) * stride + x + motion_x;
  181. prevframe_x = x + motion_x;
  182. while(pixel_count && (curframe_index < s->frame_size)) {
  183. int count = FFMIN3(pixel_count, width - curframe_x, width - prevframe_x);
  184. memcpy(palette_plane + curframe_index, prev_palette_plane + prevframe_index, count);
  185. pixel_count -= count;
  186. curframe_index += count;
  187. prevframe_index += count;
  188. curframe_x += count;
  189. prevframe_x += count;
  190. if (curframe_x >= width) {
  191. curframe_index += line_inc;
  192. curframe_x = 0;
  193. }
  194. if (prevframe_x >= width) {
  195. prevframe_index += line_inc;
  196. prevframe_x = 0;
  197. }
  198. }
  199. }
  200. static void xan_wc3_decode_frame(XanContext *s) {
  201. int width = s->avctx->width;
  202. int height = s->avctx->height;
  203. int total_pixels = width * height;
  204. unsigned char opcode;
  205. unsigned char flag = 0;
  206. int size = 0;
  207. int motion_x, motion_y;
  208. int x, y;
  209. unsigned char *opcode_buffer = s->buffer1;
  210. int opcode_buffer_size = s->buffer1_size;
  211. const unsigned char *imagedata_buffer = s->buffer2;
  212. /* pointers to segments inside the compressed chunk */
  213. const unsigned char *huffman_segment;
  214. const unsigned char *size_segment;
  215. const unsigned char *vector_segment;
  216. const unsigned char *imagedata_segment;
  217. huffman_segment = s->buf + AV_RL16(&s->buf[0]);
  218. size_segment = s->buf + AV_RL16(&s->buf[2]);
  219. vector_segment = s->buf + AV_RL16(&s->buf[4]);
  220. imagedata_segment = s->buf + AV_RL16(&s->buf[6]);
  221. xan_huffman_decode(opcode_buffer, opcode_buffer_size,
  222. huffman_segment, s->size - (huffman_segment - s->buf) );
  223. if (imagedata_segment[0] == 2)
  224. xan_unpack(s->buffer2, &imagedata_segment[1], s->buffer2_size);
  225. else
  226. imagedata_buffer = &imagedata_segment[1];
  227. /* use the decoded data segments to build the frame */
  228. x = y = 0;
  229. while (total_pixels) {
  230. opcode = *opcode_buffer++;
  231. size = 0;
  232. switch (opcode) {
  233. case 0:
  234. flag ^= 1;
  235. continue;
  236. case 1:
  237. case 2:
  238. case 3:
  239. case 4:
  240. case 5:
  241. case 6:
  242. case 7:
  243. case 8:
  244. size = opcode;
  245. break;
  246. case 12:
  247. case 13:
  248. case 14:
  249. case 15:
  250. case 16:
  251. case 17:
  252. case 18:
  253. size += (opcode - 10);
  254. break;
  255. case 9:
  256. case 19:
  257. size = *size_segment++;
  258. break;
  259. case 10:
  260. case 20:
  261. size = AV_RB16(&size_segment[0]);
  262. size_segment += 2;
  263. break;
  264. case 11:
  265. case 21:
  266. size = AV_RB24(size_segment);
  267. size_segment += 3;
  268. break;
  269. }
  270. if (opcode < 12) {
  271. flag ^= 1;
  272. if (flag) {
  273. /* run of (size) pixels is unchanged from last frame */
  274. xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
  275. } else {
  276. /* output a run of pixels from imagedata_buffer */
  277. xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);
  278. imagedata_buffer += size;
  279. }
  280. } else {
  281. /* run-based motion compensation from last frame */
  282. motion_x = sign_extend(*vector_segment >> 4, 4);
  283. motion_y = sign_extend(*vector_segment & 0xF, 4);
  284. vector_segment++;
  285. /* copy a run of pixels from the previous frame */
  286. xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
  287. flag = 0;
  288. }
  289. /* coordinate accounting */
  290. total_pixels -= size;
  291. y += (x + size) / width;
  292. x = (x + size) % width;
  293. }
  294. }
  295. static void xan_wc4_decode_frame(XanContext *s) {
  296. }
  297. static int xan_decode_frame(AVCodecContext *avctx,
  298. void *data, int *data_size,
  299. AVPacket *avpkt)
  300. {
  301. const uint8_t *buf = avpkt->data;
  302. int buf_size = avpkt->size;
  303. XanContext *s = avctx->priv_data;
  304. AVPaletteControl *palette_control = avctx->palctrl;
  305. if (avctx->get_buffer(avctx, &s->current_frame)) {
  306. av_log(s->avctx, AV_LOG_ERROR, " Xan Video: get_buffer() failed\n");
  307. return -1;
  308. }
  309. s->current_frame.reference = 3;
  310. if (!s->frame_size)
  311. s->frame_size = s->current_frame.linesize[0] * s->avctx->height;
  312. palette_control->palette_changed = 0;
  313. memcpy(s->current_frame.data[1], palette_control->palette,
  314. AVPALETTE_SIZE);
  315. s->current_frame.palette_has_changed = 1;
  316. s->buf = buf;
  317. s->size = buf_size;
  318. if (avctx->codec->id == CODEC_ID_XAN_WC3)
  319. xan_wc3_decode_frame(s);
  320. else if (avctx->codec->id == CODEC_ID_XAN_WC4)
  321. xan_wc4_decode_frame(s);
  322. /* release the last frame if it is allocated */
  323. if (s->last_frame.data[0])
  324. avctx->release_buffer(avctx, &s->last_frame);
  325. *data_size = sizeof(AVFrame);
  326. *(AVFrame*)data = s->current_frame;
  327. /* shuffle frames */
  328. FFSWAP(AVFrame, s->current_frame, s->last_frame);
  329. /* always report that the buffer was completely consumed */
  330. return buf_size;
  331. }
  332. static av_cold int xan_decode_end(AVCodecContext *avctx)
  333. {
  334. XanContext *s = avctx->priv_data;
  335. /* release the frames */
  336. if (s->last_frame.data[0])
  337. avctx->release_buffer(avctx, &s->last_frame);
  338. if (s->current_frame.data[0])
  339. avctx->release_buffer(avctx, &s->current_frame);
  340. av_freep(&s->buffer1);
  341. av_freep(&s->buffer2);
  342. return 0;
  343. }
  344. AVCodec xan_wc3_decoder = {
  345. "xan_wc3",
  346. AVMEDIA_TYPE_VIDEO,
  347. CODEC_ID_XAN_WC3,
  348. sizeof(XanContext),
  349. xan_decode_init,
  350. NULL,
  351. xan_decode_end,
  352. xan_decode_frame,
  353. CODEC_CAP_DR1,
  354. .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III / Xan"),
  355. };
  356. /*
  357. AVCodec xan_wc4_decoder = {
  358. "xan_wc4",
  359. AVMEDIA_TYPE_VIDEO,
  360. CODEC_ID_XAN_WC4,
  361. sizeof(XanContext),
  362. xan_decode_init,
  363. NULL,
  364. xan_decode_end,
  365. xan_decode_frame,
  366. CODEC_CAP_DR1,
  367. .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
  368. };
  369. */