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.

492 lines
13KB

  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. /**
  23. * @file xan.c
  24. * Xan video decoder for Wing Commander III computer game
  25. * by Mario Brito (mbrito@student.dei.uc.pt)
  26. * and Mike Melanson (melanson@pcisys.net)
  27. *
  28. * The xan_wc3 decoder outputs PAL8 data.
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include "common.h"
  35. #include "avcodec.h"
  36. typedef struct XanContext {
  37. AVCodecContext *avctx;
  38. AVFrame last_frame;
  39. AVFrame current_frame;
  40. unsigned char *buf;
  41. int size;
  42. /* scratch space */
  43. unsigned char *buffer1;
  44. int buffer1_size;
  45. unsigned char *buffer2;
  46. int buffer2_size;
  47. int frame_size;
  48. } XanContext;
  49. static int xan_decode_init(AVCodecContext *avctx)
  50. {
  51. XanContext *s = avctx->priv_data;
  52. s->avctx = avctx;
  53. s->frame_size = 0;
  54. if ((avctx->codec->id == CODEC_ID_XAN_WC3) &&
  55. (s->avctx->palctrl == NULL)) {
  56. av_log(avctx, AV_LOG_ERROR, " WC3 Xan video: palette expected.\n");
  57. return -1;
  58. }
  59. avctx->pix_fmt = PIX_FMT_PAL8;
  60. if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
  61. return -1;
  62. s->buffer1_size = avctx->width * avctx->height;
  63. s->buffer1 = av_malloc(s->buffer1_size);
  64. s->buffer2_size = avctx->width * avctx->height;
  65. s->buffer2 = av_malloc(s->buffer2_size);
  66. if (!s->buffer1 || !s->buffer2)
  67. return -1;
  68. return 0;
  69. }
  70. /* This function is used in lieu of memcpy(). This decoder cannot use
  71. * memcpy because the memory locations often overlap and
  72. * memcpy doesn't like that; it's not uncommon, for example, for
  73. * dest = src+1, to turn byte A into pattern AAAAAAAA.
  74. * This was originally repz movsb in Intel x86 ASM. */
  75. static inline void bytecopy(unsigned char *dest, unsigned char *src, int count)
  76. {
  77. int i;
  78. for (i = 0; i < count; i++)
  79. dest[i] = src[i];
  80. }
  81. static int xan_huffman_decode(unsigned char *dest, unsigned char *src,
  82. int dest_len)
  83. {
  84. unsigned char byte = *src++;
  85. unsigned char ival = byte + 0x16;
  86. unsigned char * ptr = src + byte*2;
  87. unsigned char val = ival;
  88. int counter = 0;
  89. unsigned char *dest_end = dest + dest_len;
  90. unsigned char bits = *ptr++;
  91. while ( val != 0x16 ) {
  92. if ( (1 << counter) & bits )
  93. val = src[byte + val - 0x17];
  94. else
  95. val = src[val - 0x17];
  96. if ( val < 0x16 ) {
  97. if (dest + 1 > dest_end)
  98. return 0;
  99. *dest++ = val;
  100. val = ival;
  101. }
  102. if (counter++ == 7) {
  103. counter = 0;
  104. bits = *ptr++;
  105. }
  106. }
  107. return 0;
  108. }
  109. static void xan_unpack(unsigned char *dest, unsigned char *src, int dest_len)
  110. {
  111. unsigned char opcode;
  112. int size;
  113. int offset;
  114. int byte1, byte2, byte3;
  115. unsigned char *dest_end = dest + dest_len;
  116. for (;;) {
  117. opcode = *src++;
  118. if ( (opcode & 0x80) == 0 ) {
  119. offset = *src++;
  120. size = opcode & 3;
  121. if (dest + size > dest_end)
  122. return;
  123. bytecopy(dest, src, size); dest += size; src += size;
  124. size = ((opcode & 0x1c) >> 2) + 3;
  125. if (dest + size > dest_end)
  126. return;
  127. bytecopy (dest, dest - (((opcode & 0x60) << 3) + offset + 1), size);
  128. dest += size;
  129. } else if ( (opcode & 0x40) == 0 ) {
  130. byte1 = *src++;
  131. byte2 = *src++;
  132. size = byte1 >> 6;
  133. if (dest + size > dest_end)
  134. return;
  135. bytecopy (dest, src, size); dest += size; src += size;
  136. size = (opcode & 0x3f) + 4;
  137. if (dest + size > dest_end)
  138. return;
  139. bytecopy (dest, dest - (((byte1 & 0x3f) << 8) + byte2 + 1), size);
  140. dest += size;
  141. } else if ( (opcode & 0x20) == 0 ) {
  142. byte1 = *src++;
  143. byte2 = *src++;
  144. byte3 = *src++;
  145. size = opcode & 3;
  146. if (dest + size > dest_end)
  147. return;
  148. bytecopy (dest, src, size); dest += size; src += size;
  149. size = byte3 + 5 + ((opcode & 0xc) << 6);
  150. if (dest + size > dest_end)
  151. return;
  152. bytecopy (dest,
  153. dest - ((((opcode & 0x10) >> 4) << 0x10) + 1 + (byte1 << 8) + byte2),
  154. size);
  155. dest += size;
  156. } else {
  157. size = ((opcode & 0x1f) << 2) + 4;
  158. if (size > 0x70)
  159. break;
  160. if (dest + size > dest_end)
  161. return;
  162. bytecopy (dest, src, size); dest += size; src += size;
  163. }
  164. }
  165. size = opcode & 3;
  166. bytecopy(dest, src, size); dest += size; src += size;
  167. }
  168. static inline void xan_wc3_output_pixel_run(XanContext *s,
  169. unsigned char *pixel_buffer, int x, int y, int pixel_count)
  170. {
  171. int stride;
  172. int line_inc;
  173. int index;
  174. int current_x;
  175. int width = s->avctx->width;
  176. unsigned char *palette_plane;
  177. palette_plane = s->current_frame.data[0];
  178. stride = s->current_frame.linesize[0];
  179. line_inc = stride - width;
  180. index = y * stride + x;
  181. current_x = x;
  182. while((pixel_count--) && (index < s->frame_size)) {
  183. /* don't do a memcpy() here; keyframes generally copy an entire
  184. * frame of data and the stride needs to be accounted for */
  185. palette_plane[index++] = *pixel_buffer++;
  186. current_x++;
  187. if (current_x >= width) {
  188. index += line_inc;
  189. current_x = 0;
  190. }
  191. }
  192. }
  193. static inline void xan_wc3_copy_pixel_run(XanContext *s,
  194. int x, int y, int pixel_count, int motion_x, int motion_y)
  195. {
  196. int stride;
  197. int line_inc;
  198. int curframe_index, prevframe_index;
  199. int curframe_x, prevframe_x;
  200. int width = s->avctx->width;
  201. unsigned char *palette_plane, *prev_palette_plane;
  202. palette_plane = s->current_frame.data[0];
  203. prev_palette_plane = s->last_frame.data[0];
  204. stride = s->current_frame.linesize[0];
  205. line_inc = stride - width;
  206. curframe_index = y * stride + x;
  207. curframe_x = x;
  208. prevframe_index = (y + motion_y) * stride + x + motion_x;
  209. prevframe_x = x + motion_x;
  210. while((pixel_count--) && (curframe_index < s->frame_size)) {
  211. palette_plane[curframe_index++] =
  212. prev_palette_plane[prevframe_index++];
  213. curframe_x++;
  214. if (curframe_x >= width) {
  215. curframe_index += line_inc;
  216. curframe_x = 0;
  217. }
  218. prevframe_x++;
  219. if (prevframe_x >= width) {
  220. prevframe_index += line_inc;
  221. prevframe_x = 0;
  222. }
  223. }
  224. }
  225. static void xan_wc3_decode_frame(XanContext *s) {
  226. int width = s->avctx->width;
  227. int height = s->avctx->height;
  228. int total_pixels = width * height;
  229. unsigned char opcode;
  230. unsigned char flag = 0;
  231. int size = 0;
  232. int motion_x, motion_y;
  233. int x, y;
  234. unsigned char *opcode_buffer = s->buffer1;
  235. int opcode_buffer_size = s->buffer1_size;
  236. unsigned char *imagedata_buffer = s->buffer2;
  237. int imagedata_buffer_size = s->buffer2_size;
  238. /* pointers to segments inside the compressed chunk */
  239. unsigned char *huffman_segment;
  240. unsigned char *size_segment;
  241. unsigned char *vector_segment;
  242. unsigned char *imagedata_segment;
  243. huffman_segment = s->buf + AV_RL16(&s->buf[0]);
  244. size_segment = s->buf + AV_RL16(&s->buf[2]);
  245. vector_segment = s->buf + AV_RL16(&s->buf[4]);
  246. imagedata_segment = s->buf + AV_RL16(&s->buf[6]);
  247. xan_huffman_decode(opcode_buffer, huffman_segment, opcode_buffer_size);
  248. if (imagedata_segment[0] == 2)
  249. xan_unpack(imagedata_buffer, &imagedata_segment[1],
  250. imagedata_buffer_size);
  251. else
  252. imagedata_buffer = &imagedata_segment[1];
  253. /* use the decoded data segments to build the frame */
  254. x = y = 0;
  255. while (total_pixels) {
  256. opcode = *opcode_buffer++;
  257. size = 0;
  258. switch (opcode) {
  259. case 0:
  260. flag ^= 1;
  261. continue;
  262. case 1:
  263. case 2:
  264. case 3:
  265. case 4:
  266. case 5:
  267. case 6:
  268. case 7:
  269. case 8:
  270. size = opcode;
  271. break;
  272. case 12:
  273. case 13:
  274. case 14:
  275. case 15:
  276. case 16:
  277. case 17:
  278. case 18:
  279. size += (opcode - 10);
  280. break;
  281. case 9:
  282. case 19:
  283. size = *size_segment++;
  284. break;
  285. case 10:
  286. case 20:
  287. size = AV_RB16(&size_segment[0]);
  288. size_segment += 2;
  289. break;
  290. case 11:
  291. case 21:
  292. size = (size_segment[0] << 16) | (size_segment[1] << 8) |
  293. size_segment[2];
  294. size_segment += 3;
  295. break;
  296. }
  297. if (opcode < 12) {
  298. flag ^= 1;
  299. if (flag) {
  300. /* run of (size) pixels is unchanged from last frame */
  301. xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
  302. } else {
  303. /* output a run of pixels from imagedata_buffer */
  304. xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);
  305. imagedata_buffer += size;
  306. }
  307. } else {
  308. /* run-based motion compensation from last frame */
  309. motion_x = (*vector_segment >> 4) & 0xF;
  310. motion_y = *vector_segment & 0xF;
  311. vector_segment++;
  312. /* sign extension */
  313. if (motion_x & 0x8)
  314. motion_x |= 0xFFFFFFF0;
  315. if (motion_y & 0x8)
  316. motion_y |= 0xFFFFFFF0;
  317. /* copy a run of pixels from the previous frame */
  318. xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
  319. flag = 0;
  320. }
  321. /* coordinate accounting */
  322. total_pixels -= size;
  323. while (size) {
  324. if (x + size >= width) {
  325. y++;
  326. size -= (width - x);
  327. x = 0;
  328. } else {
  329. x += size;
  330. size = 0;
  331. }
  332. }
  333. }
  334. }
  335. static void xan_wc4_decode_frame(XanContext *s) {
  336. }
  337. static int xan_decode_frame(AVCodecContext *avctx,
  338. void *data, int *data_size,
  339. uint8_t *buf, int buf_size)
  340. {
  341. XanContext *s = avctx->priv_data;
  342. AVPaletteControl *palette_control = avctx->palctrl;
  343. if (avctx->get_buffer(avctx, &s->current_frame)) {
  344. av_log(s->avctx, AV_LOG_ERROR, " Xan Video: get_buffer() failed\n");
  345. return -1;
  346. }
  347. s->current_frame.reference = 3;
  348. if (!s->frame_size)
  349. s->frame_size = s->current_frame.linesize[0] * s->avctx->height;
  350. palette_control->palette_changed = 0;
  351. memcpy(s->current_frame.data[1], palette_control->palette,
  352. AVPALETTE_SIZE);
  353. s->current_frame.palette_has_changed = 1;
  354. s->buf = buf;
  355. s->size = buf_size;
  356. if (avctx->codec->id == CODEC_ID_XAN_WC3)
  357. xan_wc3_decode_frame(s);
  358. else if (avctx->codec->id == CODEC_ID_XAN_WC4)
  359. xan_wc4_decode_frame(s);
  360. /* release the last frame if it is allocated */
  361. if (s->last_frame.data[0])
  362. avctx->release_buffer(avctx, &s->last_frame);
  363. /* shuffle frames */
  364. s->last_frame = s->current_frame;
  365. *data_size = sizeof(AVFrame);
  366. *(AVFrame*)data = s->current_frame;
  367. /* always report that the buffer was completely consumed */
  368. return buf_size;
  369. }
  370. static int xan_decode_end(AVCodecContext *avctx)
  371. {
  372. XanContext *s = avctx->priv_data;
  373. /* release the last frame */
  374. if (s->last_frame.data[0])
  375. avctx->release_buffer(avctx, &s->last_frame);
  376. av_free(s->buffer1);
  377. av_free(s->buffer2);
  378. return 0;
  379. }
  380. AVCodec xan_wc3_decoder = {
  381. "xan_wc3",
  382. CODEC_TYPE_VIDEO,
  383. CODEC_ID_XAN_WC3,
  384. sizeof(XanContext),
  385. xan_decode_init,
  386. NULL,
  387. xan_decode_end,
  388. xan_decode_frame,
  389. CODEC_CAP_DR1,
  390. };
  391. /*
  392. AVCodec xan_wc4_decoder = {
  393. "xan_wc4",
  394. CODEC_TYPE_VIDEO,
  395. CODEC_ID_XAN_WC4,
  396. sizeof(XanContext),
  397. xan_decode_init,
  398. NULL,
  399. xan_decode_end,
  400. xan_decode_frame,
  401. CODEC_CAP_DR1,
  402. };
  403. */