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.

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