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.

487 lines
13KB

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