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.

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