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.

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