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.

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