xan.c 13 KB

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