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.

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