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.

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