xan.c 13 KB

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