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.

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