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.

606 lines
16KB

  1. /*
  2. * Wing Commander/Xan Video Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file xan.c
  22. * Xan video decoder for Wing Commander III & IV computer games
  23. * by Mario Brito (mbrito@student.dei.uc.pt)
  24. * and Mike Melanson (melanson@pcisys.net)
  25. * For more information about the Xan format, visit:
  26. * http://www.pcisys.net/~melanson/codecs/
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include "common.h"
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #define PALETTE_COUNT 256
  36. #define PALETTE_CONTROL_SIZE ((256 * 3) + 1)
  37. typedef struct XanContext {
  38. AVCodecContext *avctx;
  39. DSPContext dsp;
  40. AVFrame last_frame;
  41. AVFrame current_frame;
  42. unsigned char *buf;
  43. int size;
  44. unsigned char palette[PALETTE_COUNT * 4];
  45. /* scratch space */
  46. unsigned char *buffer1;
  47. unsigned char *buffer2;
  48. } XanContext;
  49. #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
  50. #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  51. #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
  52. (((uint8_t*)(x))[2] << 16) | \
  53. (((uint8_t*)(x))[1] << 8) | \
  54. ((uint8_t*)(x))[0])
  55. /* RGB -> YUV conversion stuff */
  56. #define SCALEFACTOR 65536
  57. #define CENTERSAMPLE 128
  58. #define COMPUTE_Y(r, g, b) \
  59. (unsigned char) \
  60. ((y_r_table[r] + y_g_table[g] + y_b_table[b]) / SCALEFACTOR)
  61. #define COMPUTE_U(r, g, b) \
  62. (unsigned char) \
  63. ((u_r_table[r] + u_g_table[g] + u_b_table[b]) / SCALEFACTOR + CENTERSAMPLE)
  64. #define COMPUTE_V(r, g, b) \
  65. (unsigned char) \
  66. ((v_r_table[r] + v_g_table[g] + v_b_table[b]) / SCALEFACTOR + CENTERSAMPLE)
  67. #define Y_R (SCALEFACTOR * 0.29900)
  68. #define Y_G (SCALEFACTOR * 0.58700)
  69. #define Y_B (SCALEFACTOR * 0.11400)
  70. #define U_R (SCALEFACTOR * -0.16874)
  71. #define U_G (SCALEFACTOR * -0.33126)
  72. #define U_B (SCALEFACTOR * 0.50000)
  73. #define V_R (SCALEFACTOR * 0.50000)
  74. #define V_G (SCALEFACTOR * -0.41869)
  75. #define V_B (SCALEFACTOR * -0.08131)
  76. /*
  77. * Precalculate all of the YUV tables since it requires fewer than
  78. * 10 kilobytes to store them.
  79. */
  80. static int y_r_table[256];
  81. static int y_g_table[256];
  82. static int y_b_table[256];
  83. static int u_r_table[256];
  84. static int u_g_table[256];
  85. static int u_b_table[256];
  86. static int v_r_table[256];
  87. static int v_g_table[256];
  88. static int v_b_table[256];
  89. static int xan_decode_init(AVCodecContext *avctx)
  90. {
  91. XanContext *s = avctx->priv_data;
  92. int i;
  93. s->avctx = avctx;
  94. if ((avctx->codec->id == CODEC_ID_XAN_WC3) &&
  95. (s->avctx->extradata_size != PALETTE_CONTROL_SIZE)) {
  96. printf (" WC3 Xan video: expected extradata_size of %d\n",
  97. PALETTE_CONTROL_SIZE);
  98. return -1;
  99. }
  100. avctx->pix_fmt = PIX_FMT_YUV444P;
  101. avctx->has_b_frames = 0;
  102. dsputil_init(&s->dsp, avctx);
  103. /* initialize the RGB -> YUV tables */
  104. for (i = 0; i < 256; i++) {
  105. y_r_table[i] = Y_R * i;
  106. y_g_table[i] = Y_G * i;
  107. y_b_table[i] = Y_B * i;
  108. u_r_table[i] = U_R * i;
  109. u_g_table[i] = U_G * i;
  110. u_b_table[i] = U_B * i;
  111. v_r_table[i] = V_R * i;
  112. v_g_table[i] = V_G * i;
  113. v_b_table[i] = V_B * i;
  114. }
  115. s->buffer1 = av_malloc(avctx->width * avctx->height * 4);
  116. s->buffer2 = av_malloc(avctx->width * avctx->height * 4);
  117. if (!s->buffer1 || !s->buffer2)
  118. return -1;
  119. return 0;
  120. }
  121. /* This function is used in lieu of memcpy(). This decoder can not use
  122. * memcpy because the memory locations often overlap and
  123. * memcpy doesn't like that; it's not uncommon, for example, for
  124. * dest = src+1, to turn byte A into pattern AAAAAAAA.
  125. * This was originally repz movsb in Intel x86 ASM. */
  126. static inline void bytecopy(unsigned char *dest, unsigned char *src, int count)
  127. {
  128. int i;
  129. for (i = 0; i < count; i++)
  130. dest[i] = src[i];
  131. }
  132. static int xan_decode_method_1(unsigned char *dest, unsigned char *src)
  133. {
  134. unsigned char byte = *src++;
  135. unsigned char ival = byte + 0x16;
  136. unsigned char * ptr = src + byte*2;
  137. unsigned char val = ival;
  138. int counter = 0;
  139. unsigned char bits = *ptr++;
  140. while ( val != 0x16 ) {
  141. if ( (1 << counter) & bits )
  142. val = src[byte + val - 0x17];
  143. else
  144. val = src[val - 0x17];
  145. if ( val < 0x16 ) {
  146. *dest++ = val;
  147. val = ival;
  148. }
  149. if (counter++ == 7) {
  150. counter = 0;
  151. bits = *ptr++;
  152. }
  153. }
  154. return 0;
  155. }
  156. static int xan_decode_method_2(unsigned char *dest, unsigned char *src)
  157. {
  158. unsigned char opcode;
  159. int size;
  160. int offset;
  161. int byte1, byte2, byte3;
  162. for (;;) {
  163. opcode = *src++;
  164. if ( (opcode & 0x80) == 0 ) {
  165. offset = *src++;
  166. size = opcode & 3;
  167. bytecopy(dest, src, size); dest += size; src += size;
  168. size = ((opcode & 0x1c) >> 2) + 3;
  169. bytecopy (dest, dest - (((opcode & 0x60) << 3) + offset + 1), size);
  170. dest += size;
  171. } else if ( (opcode & 0x40) == 0 ) {
  172. byte1 = *src++;
  173. byte2 = *src++;
  174. size = byte1 >> 6;
  175. bytecopy (dest, src, size); dest += size; src += size;
  176. size = (opcode & 0x3f) + 4;
  177. bytecopy (dest, dest - (((byte1 & 0x3f) << 8) + byte2 + 1), size);
  178. dest += size;
  179. } else if ( (opcode & 0x20) == 0 ) {
  180. byte1 = *src++;
  181. byte2 = *src++;
  182. byte3 = *src++;
  183. size = opcode & 3;
  184. bytecopy (dest, src, size); dest += size; src += size;
  185. size = byte3 + 5 + ((opcode & 0xc) << 6);
  186. bytecopy (dest,
  187. dest - ((((opcode & 0x10) >> 4) << 0x10) + 1 + (byte1 << 8) + byte2),
  188. size);
  189. dest += size;
  190. } else {
  191. size = ((opcode & 0x1f) << 2) + 4;
  192. if (size > 0x70)
  193. break;
  194. bytecopy (dest, src, size); dest += size; src += size;
  195. }
  196. }
  197. size = opcode & 3;
  198. bytecopy(dest, src, size); dest += size; src += size;
  199. return 0;
  200. }
  201. static void inline xan_wc3_build_palette(XanContext *s,
  202. unsigned char *palette_data)
  203. {
  204. int i;
  205. unsigned char r, g, b;
  206. /* transform the palette passed through the palette control structure
  207. * into the necessary internal format depending on colorspace */
  208. switch (s->avctx->pix_fmt) {
  209. case PIX_FMT_YUV444P:
  210. for (i = 0; i < PALETTE_COUNT; i++) {
  211. r = *palette_data++;
  212. g = *palette_data++;
  213. b = *palette_data++;
  214. s->palette[i * 4 + 0] = COMPUTE_Y(r, g, b);
  215. s->palette[i * 4 + 1] = COMPUTE_U(r, g, b);
  216. s->palette[i * 4 + 2] = COMPUTE_V(r, g, b);
  217. }
  218. break;
  219. default:
  220. printf (" Xan WC3: Unhandled colorspace\n");
  221. break;
  222. }
  223. }
  224. static void inline xan_wc3_output_pixel_run(XanContext *s,
  225. unsigned char *pixel_buffer, int x, int y, int pixel_count)
  226. {
  227. int stride;
  228. int line_inc;
  229. int index;
  230. int current_x;
  231. int width = s->avctx->width;
  232. unsigned char pixel;
  233. unsigned char *y_plane;
  234. unsigned char *u_plane;
  235. unsigned char *v_plane;
  236. switch (s->avctx->pix_fmt) {
  237. case PIX_FMT_YUV444P:
  238. y_plane = s->current_frame.data[0];
  239. u_plane = s->current_frame.data[1];
  240. v_plane = s->current_frame.data[2];
  241. stride = s->current_frame.linesize[0];
  242. line_inc = stride - width;
  243. index = y * stride + x;
  244. current_x = x;
  245. while(pixel_count--) {
  246. pixel = *pixel_buffer++;
  247. y_plane[index] = s->palette[pixel * 4 + 0];
  248. u_plane[index] = s->palette[pixel * 4 + 1];
  249. v_plane[index] = s->palette[pixel * 4 + 2];
  250. index++;
  251. current_x++;
  252. if (current_x >= width) {
  253. /* reset accounting variables */
  254. index += line_inc;
  255. current_x = 0;
  256. }
  257. }
  258. break;
  259. default:
  260. printf (" Xan WC3: Unhandled colorspace\n");
  261. break;
  262. }
  263. }
  264. static void inline xan_wc3_copy_pixel_run(XanContext *s,
  265. int x, int y, int pixel_count, int motion_x, int motion_y)
  266. {
  267. int stride;
  268. int line_inc;
  269. int curframe_index, prevframe_index;
  270. int curframe_x, prevframe_x;
  271. int width = s->avctx->width;
  272. unsigned char *y_plane, *u_plane, *v_plane;
  273. unsigned char *prev_y_plane, *prev_u_plane, *prev_v_plane;
  274. switch (s->avctx->pix_fmt) {
  275. case PIX_FMT_YUV444P:
  276. y_plane = s->current_frame.data[0];
  277. u_plane = s->current_frame.data[1];
  278. v_plane = s->current_frame.data[2];
  279. prev_y_plane = s->last_frame.data[0];
  280. prev_u_plane = s->last_frame.data[1];
  281. prev_v_plane = s->last_frame.data[2];
  282. stride = s->current_frame.linesize[0];
  283. line_inc = stride - width;
  284. curframe_index = y * stride + x;
  285. curframe_x = x;
  286. prevframe_index = (y + motion_x) * stride + x + motion_x;
  287. prevframe_x = x + motion_x;
  288. while(pixel_count--) {
  289. y_plane[curframe_index] = prev_y_plane[prevframe_index];
  290. u_plane[curframe_index] = prev_u_plane[prevframe_index];
  291. v_plane[curframe_index] = prev_v_plane[prevframe_index];
  292. curframe_index++;
  293. curframe_x++;
  294. if (curframe_x >= width) {
  295. /* reset accounting variables */
  296. curframe_index += line_inc;
  297. curframe_x = 0;
  298. }
  299. prevframe_index++;
  300. prevframe_x++;
  301. if (prevframe_x >= width) {
  302. /* reset accounting variables */
  303. prevframe_index += line_inc;
  304. prevframe_x = 0;
  305. }
  306. }
  307. break;
  308. default:
  309. printf (" Xan WC3: Unhandled colorspace\n");
  310. break;
  311. }
  312. }
  313. static void xan_wc3_decode_frame(XanContext *s) {
  314. int width = s->avctx->width;
  315. int height = s->avctx->height;
  316. int total_pixels = width * height;
  317. unsigned char opcode;
  318. unsigned char flag = 0;
  319. int size = 0;
  320. int motion_x, motion_y;
  321. int x, y;
  322. unsigned char *method1_buffer = s->buffer1;
  323. unsigned char *method2_buffer = s->buffer2;
  324. /* pointers to segments inside the compressed chunk */
  325. unsigned char *method1_segment;
  326. unsigned char *size_segment;
  327. unsigned char *vector_segment;
  328. unsigned char *method2_segment;
  329. method1_segment = s->buf + LE_16(&s->buf[0]);
  330. size_segment = s->buf + LE_16(&s->buf[2]);
  331. vector_segment = s->buf + LE_16(&s->buf[4]);
  332. method2_segment = s->buf + LE_16(&s->buf[6]);
  333. xan_decode_method_1(method1_buffer, method1_segment);
  334. if (method2_segment[0] == 2)
  335. xan_decode_method_2(method2_buffer, method2_segment + 1);
  336. else
  337. method2_buffer = method2_segment + 1;
  338. /* use the decoded data segments to build the frame */
  339. x = y = 0;
  340. while (total_pixels) {
  341. opcode = *method1_buffer++;
  342. size = 0;
  343. switch (opcode) {
  344. case 0:
  345. flag ^= 1;
  346. continue;
  347. case 1:
  348. case 2:
  349. case 3:
  350. case 4:
  351. case 5:
  352. case 6:
  353. case 7:
  354. case 8:
  355. size = opcode;
  356. break;
  357. case 12:
  358. case 13:
  359. case 14:
  360. case 15:
  361. case 16:
  362. case 17:
  363. case 18:
  364. size += (opcode - 10);
  365. break;
  366. case 9:
  367. case 19:
  368. size = *size_segment++;
  369. break;
  370. case 10:
  371. case 20:
  372. size = BE_16(&size_segment[0]);
  373. size_segment += 2;
  374. break;
  375. case 11:
  376. case 21:
  377. size = (size_segment[0] << 16) | (size_segment[1] << 8) |
  378. size_segment[2];
  379. size_segment += 3;
  380. break;
  381. }
  382. if (opcode < 12) {
  383. flag ^= 1;
  384. if (flag) {
  385. /* run of (size) pixels is unchanged from last frame */
  386. xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
  387. } else {
  388. /* output a run of pixels from method2_buffer */
  389. xan_wc3_output_pixel_run(s, method2_buffer, x, y, size);
  390. method2_buffer += size;
  391. }
  392. } else {
  393. /* run-based motion compensation from last frame */
  394. motion_x = (*vector_segment >> 4) & 0xF;
  395. motion_y = *vector_segment & 0xF;
  396. vector_segment++;
  397. /* sign extension */
  398. if (motion_x & 0x8)
  399. motion_x |= 0xFFFFFFF0;
  400. if (motion_y & 0x8)
  401. motion_y |= 0xFFFFFFF0;
  402. /* copy a run of pixels from the previous frame */
  403. xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
  404. flag = 0;
  405. }
  406. /* coordinate accounting */
  407. total_pixels -= size;
  408. while (size) {
  409. if (x + size >= width) {
  410. y++;
  411. size -= (width - x);
  412. x = 0;
  413. } else {
  414. x += size;
  415. size = 0;
  416. }
  417. }
  418. }
  419. }
  420. static void xan_wc4_decode_frame(XanContext *s) {
  421. }
  422. static int xan_decode_frame(AVCodecContext *avctx,
  423. void *data, int *data_size,
  424. uint8_t *buf, int buf_size)
  425. {
  426. XanContext *s = avctx->priv_data;
  427. unsigned char *palette_control = avctx->extradata;
  428. int keyframe = 0;
  429. if (palette_control[0]) {
  430. /* load the new palette and reset the palette control */
  431. xan_wc3_build_palette(s, &palette_control[1]);
  432. palette_control[0] = 0;
  433. keyframe = 1;
  434. }
  435. if (avctx->get_buffer(avctx, &s->current_frame)) {
  436. printf (" Interplay Video: get_buffer() failed\n");
  437. return -1;
  438. }
  439. s->buf = buf;
  440. s->size = buf_size;
  441. if (avctx->codec->id == CODEC_ID_XAN_WC3) {
  442. // if (keyframe)
  443. if (1)
  444. xan_wc3_decode_frame(s);
  445. else {
  446. memcpy(s->current_frame.data[0], s->last_frame.data[0],
  447. s->current_frame.linesize[0] * avctx->height);
  448. memcpy(s->current_frame.data[1], s->last_frame.data[1],
  449. s->current_frame.linesize[1] * avctx->height);
  450. memcpy(s->current_frame.data[2], s->last_frame.data[2],
  451. s->current_frame.linesize[2] * avctx->height);
  452. }
  453. } else if (avctx->codec->id == CODEC_ID_XAN_WC4)
  454. xan_wc4_decode_frame(s);
  455. /* release the last frame if it is allocated */
  456. if (s->last_frame.data[0])
  457. avctx->release_buffer(avctx, &s->last_frame);
  458. /* shuffle frames */
  459. s->last_frame = s->current_frame;
  460. *data_size = sizeof(AVFrame);
  461. *(AVFrame*)data = s->current_frame;
  462. /* always report that the buffer was completely consumed */
  463. return buf_size;
  464. }
  465. static int xan_decode_end(AVCodecContext *avctx)
  466. {
  467. XanContext *s = avctx->priv_data;
  468. /* release the last frame */
  469. avctx->release_buffer(avctx, &s->last_frame);
  470. av_free(s->buffer1);
  471. av_free(s->buffer2);
  472. return 0;
  473. }
  474. AVCodec xan_wc3_decoder = {
  475. "xan_wc3",
  476. CODEC_TYPE_VIDEO,
  477. CODEC_ID_XAN_WC3,
  478. sizeof(XanContext),
  479. xan_decode_init,
  480. NULL,
  481. xan_decode_end,
  482. xan_decode_frame,
  483. CODEC_CAP_DR1,
  484. };
  485. /*
  486. AVCodec xan_wc4_decoder = {
  487. "xan_wc4",
  488. CODEC_TYPE_VIDEO,
  489. CODEC_ID_XAN_WC4,
  490. sizeof(XanContext),
  491. xan_decode_init,
  492. NULL,
  493. xan_decode_end,
  494. xan_decode_frame,
  495. CODEC_CAP_DR1,
  496. };
  497. */