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.

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