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.

825 lines
23KB

  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. *
  26. * The xan_wc3 decoder outputs the following colorspaces natively:
  27. * PAL8 (default), RGB555, RGB565, RGB24, BGR24, RGBA32, YUV444P
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include "common.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #define PALETTE_COUNT 256
  37. #define PALETTE_CONTROL_SIZE ((256 * 3) + 1)
  38. typedef struct XanContext {
  39. AVCodecContext *avctx;
  40. DSPContext dsp;
  41. AVFrame last_frame;
  42. AVFrame current_frame;
  43. unsigned char *buf;
  44. int size;
  45. unsigned char palette[PALETTE_COUNT * 4];
  46. /* scratch space */
  47. unsigned char *buffer1;
  48. unsigned char *buffer2;
  49. } XanContext;
  50. /* RGB -> YUV conversion stuff */
  51. #define SCALEFACTOR 65536
  52. #define CENTERSAMPLE 128
  53. #define COMPUTE_Y(r, g, b) \
  54. (unsigned char) \
  55. ((y_r_table[r] + y_g_table[g] + y_b_table[b]) / SCALEFACTOR)
  56. #define COMPUTE_U(r, g, b) \
  57. (unsigned char) \
  58. ((u_r_table[r] + u_g_table[g] + u_b_table[b]) / SCALEFACTOR + CENTERSAMPLE)
  59. #define COMPUTE_V(r, g, b) \
  60. (unsigned char) \
  61. ((v_r_table[r] + v_g_table[g] + v_b_table[b]) / SCALEFACTOR + CENTERSAMPLE)
  62. #define Y_R (SCALEFACTOR * 0.29900)
  63. #define Y_G (SCALEFACTOR * 0.58700)
  64. #define Y_B (SCALEFACTOR * 0.11400)
  65. #define U_R (SCALEFACTOR * -0.16874)
  66. #define U_G (SCALEFACTOR * -0.33126)
  67. #define U_B (SCALEFACTOR * 0.50000)
  68. #define V_R (SCALEFACTOR * 0.50000)
  69. #define V_G (SCALEFACTOR * -0.41869)
  70. #define V_B (SCALEFACTOR * -0.08131)
  71. /*
  72. * Precalculate all of the YUV tables since it requires fewer than
  73. * 10 kilobytes to store them.
  74. */
  75. static int y_r_table[256];
  76. static int y_g_table[256];
  77. static int y_b_table[256];
  78. static int u_r_table[256];
  79. static int u_g_table[256];
  80. static int u_b_table[256];
  81. static int v_r_table[256];
  82. static int v_g_table[256];
  83. static int v_b_table[256];
  84. static int xan_decode_init(AVCodecContext *avctx)
  85. {
  86. XanContext *s = avctx->priv_data;
  87. int i;
  88. s->avctx = avctx;
  89. if ((avctx->codec->id == CODEC_ID_XAN_WC3) &&
  90. (s->avctx->palctrl == NULL)) {
  91. av_log(avctx, AV_LOG_ERROR, " WC3 Xan video: palette expected.\n");
  92. return -1;
  93. }
  94. avctx->pix_fmt = PIX_FMT_PAL8;
  95. avctx->has_b_frames = 0;
  96. dsputil_init(&s->dsp, avctx);
  97. /* initialize the RGB -> YUV tables */
  98. for (i = 0; i < 256; i++) {
  99. y_r_table[i] = Y_R * i;
  100. y_g_table[i] = Y_G * i;
  101. y_b_table[i] = Y_B * i;
  102. u_r_table[i] = U_R * i;
  103. u_g_table[i] = U_G * i;
  104. u_b_table[i] = U_B * i;
  105. v_r_table[i] = V_R * i;
  106. v_g_table[i] = V_G * i;
  107. v_b_table[i] = V_B * i;
  108. }
  109. if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
  110. return -1;
  111. s->buffer1 = av_malloc(avctx->width * avctx->height);
  112. s->buffer2 = av_malloc(avctx->width * avctx->height);
  113. if (!s->buffer1 || !s->buffer2)
  114. return -1;
  115. return 0;
  116. }
  117. /* This function is used in lieu of memcpy(). This decoder can not use
  118. * memcpy because the memory locations often overlap and
  119. * memcpy doesn't like that; it's not uncommon, for example, for
  120. * dest = src+1, to turn byte A into pattern AAAAAAAA.
  121. * This was originally repz movsb in Intel x86 ASM. */
  122. static inline void bytecopy(unsigned char *dest, unsigned char *src, int count)
  123. {
  124. int i;
  125. for (i = 0; i < count; i++)
  126. dest[i] = src[i];
  127. }
  128. static int xan_huffman_decode(unsigned char *dest, unsigned char *src)
  129. {
  130. unsigned char byte = *src++;
  131. unsigned char ival = byte + 0x16;
  132. unsigned char * ptr = src + byte*2;
  133. unsigned char val = ival;
  134. int counter = 0;
  135. unsigned char bits = *ptr++;
  136. while ( val != 0x16 ) {
  137. if ( (1 << counter) & bits )
  138. val = src[byte + val - 0x17];
  139. else
  140. val = src[val - 0x17];
  141. if ( val < 0x16 ) {
  142. *dest++ = val;
  143. val = ival;
  144. }
  145. if (counter++ == 7) {
  146. counter = 0;
  147. bits = *ptr++;
  148. }
  149. }
  150. return 0;
  151. }
  152. static void xan_unpack(unsigned char *dest, unsigned char *src)
  153. {
  154. unsigned char opcode;
  155. int size;
  156. int offset;
  157. int byte1, byte2, byte3;
  158. for (;;) {
  159. opcode = *src++;
  160. if ( (opcode & 0x80) == 0 ) {
  161. offset = *src++;
  162. size = opcode & 3;
  163. bytecopy(dest, src, size); dest += size; src += size;
  164. size = ((opcode & 0x1c) >> 2) + 3;
  165. bytecopy (dest, dest - (((opcode & 0x60) << 3) + offset + 1), size);
  166. dest += size;
  167. } else if ( (opcode & 0x40) == 0 ) {
  168. byte1 = *src++;
  169. byte2 = *src++;
  170. size = byte1 >> 6;
  171. bytecopy (dest, src, size); dest += size; src += size;
  172. size = (opcode & 0x3f) + 4;
  173. bytecopy (dest, dest - (((byte1 & 0x3f) << 8) + byte2 + 1), size);
  174. dest += size;
  175. } else if ( (opcode & 0x20) == 0 ) {
  176. byte1 = *src++;
  177. byte2 = *src++;
  178. byte3 = *src++;
  179. size = opcode & 3;
  180. bytecopy (dest, src, size); dest += size; src += size;
  181. size = byte3 + 5 + ((opcode & 0xc) << 6);
  182. bytecopy (dest,
  183. dest - ((((opcode & 0x10) >> 4) << 0x10) + 1 + (byte1 << 8) + byte2),
  184. size);
  185. dest += size;
  186. } else {
  187. size = ((opcode & 0x1f) << 2) + 4;
  188. if (size > 0x70)
  189. break;
  190. bytecopy (dest, src, size); dest += size; src += size;
  191. }
  192. }
  193. size = opcode & 3;
  194. bytecopy(dest, src, size); dest += size; src += size;
  195. }
  196. static void inline xan_wc3_build_palette(XanContext *s,
  197. unsigned int *palette_data)
  198. {
  199. int i;
  200. unsigned char r, g, b;
  201. unsigned short *palette16;
  202. unsigned int *palette32;
  203. unsigned int pal_elem;
  204. /* transform the palette passed through the palette control structure
  205. * into the necessary internal format depending on colorspace */
  206. switch (s->avctx->pix_fmt) {
  207. case PIX_FMT_RGB555:
  208. palette16 = (unsigned short *)s->palette;
  209. for (i = 0; i < PALETTE_COUNT; i++) {
  210. pal_elem = palette_data[i];
  211. r = (pal_elem >> 16) & 0xff;
  212. g = (pal_elem >> 8) & 0xff;
  213. b = pal_elem & 0xff;
  214. palette16[i] =
  215. ((r >> 3) << 10) |
  216. ((g >> 3) << 5) |
  217. ((b >> 3) << 0);
  218. }
  219. break;
  220. case PIX_FMT_RGB565:
  221. palette16 = (unsigned short *)s->palette;
  222. for (i = 0; i < PALETTE_COUNT; i++) {
  223. pal_elem = palette_data[i];
  224. r = (pal_elem >> 16) & 0xff;
  225. g = (pal_elem >> 8) & 0xff;
  226. b = pal_elem & 0xff;
  227. palette16[i] =
  228. ((r >> 3) << 11) |
  229. ((g >> 2) << 5) |
  230. ((b >> 3) << 0);
  231. }
  232. break;
  233. case PIX_FMT_RGB24:
  234. for (i = 0; i < PALETTE_COUNT; i++) {
  235. pal_elem = palette_data[i];
  236. r = (pal_elem >> 16) & 0xff;
  237. g = (pal_elem >> 8) & 0xff;
  238. b = pal_elem & 0xff;
  239. s->palette[i * 4 + 0] = r;
  240. s->palette[i * 4 + 1] = g;
  241. s->palette[i * 4 + 2] = b;
  242. }
  243. break;
  244. case PIX_FMT_BGR24:
  245. for (i = 0; i < PALETTE_COUNT; i++) {
  246. pal_elem = palette_data[i];
  247. r = (pal_elem >> 16) & 0xff;
  248. g = (pal_elem >> 8) & 0xff;
  249. b = pal_elem & 0xff;
  250. s->palette[i * 4 + 0] = b;
  251. s->palette[i * 4 + 1] = g;
  252. s->palette[i * 4 + 2] = r;
  253. }
  254. break;
  255. case PIX_FMT_PAL8:
  256. case PIX_FMT_RGBA32:
  257. palette32 = (unsigned int *)s->palette;
  258. memcpy (palette32, palette_data, PALETTE_COUNT * sizeof(unsigned int));
  259. break;
  260. case PIX_FMT_YUV444P:
  261. for (i = 0; i < PALETTE_COUNT; i++) {
  262. pal_elem = palette_data[i];
  263. r = (pal_elem >> 16) & 0xff;
  264. g = (pal_elem >> 8) & 0xff;
  265. b = pal_elem & 0xff;
  266. s->palette[i * 4 + 0] = COMPUTE_Y(r, g, b);
  267. s->palette[i * 4 + 1] = COMPUTE_U(r, g, b);
  268. s->palette[i * 4 + 2] = COMPUTE_V(r, g, b);
  269. }
  270. break;
  271. default:
  272. av_log(s->avctx, AV_LOG_ERROR, " Xan WC3: Unhandled colorspace\n");
  273. break;
  274. }
  275. }
  276. /* advance current_x variable; reset accounting variables if current_x
  277. * moves beyond width */
  278. #define ADVANCE_CURRENT_X() \
  279. current_x++; \
  280. if (current_x >= width) { \
  281. index += line_inc; \
  282. current_x = 0; \
  283. }
  284. static void inline xan_wc3_output_pixel_run(XanContext *s,
  285. unsigned char *pixel_buffer, int x, int y, int pixel_count)
  286. {
  287. int stride;
  288. int line_inc;
  289. int index;
  290. int current_x;
  291. int width = s->avctx->width;
  292. unsigned char pix;
  293. unsigned char *palette_plane;
  294. unsigned char *y_plane;
  295. unsigned char *u_plane;
  296. unsigned char *v_plane;
  297. unsigned char *rgb_plane;
  298. unsigned short *rgb16_plane;
  299. unsigned short *palette16;
  300. unsigned int *rgb32_plane;
  301. unsigned int *palette32;
  302. switch (s->avctx->pix_fmt) {
  303. case PIX_FMT_PAL8:
  304. palette_plane = s->current_frame.data[0];
  305. stride = s->current_frame.linesize[0];
  306. line_inc = stride - width;
  307. index = y * stride + x;
  308. current_x = x;
  309. while(pixel_count--) {
  310. /* don't do a memcpy() here; keyframes generally copy an entire
  311. * frame of data and the stride needs to be accounted for */
  312. palette_plane[index++] = *pixel_buffer++;
  313. ADVANCE_CURRENT_X();
  314. }
  315. break;
  316. case PIX_FMT_RGB555:
  317. case PIX_FMT_RGB565:
  318. rgb16_plane = (unsigned short *)s->current_frame.data[0];
  319. palette16 = (unsigned short *)s->palette;
  320. stride = s->current_frame.linesize[0] / 2;
  321. line_inc = stride - width;
  322. index = y * stride + x;
  323. current_x = x;
  324. while(pixel_count--) {
  325. rgb16_plane[index++] = palette16[*pixel_buffer++];
  326. ADVANCE_CURRENT_X();
  327. }
  328. break;
  329. case PIX_FMT_RGB24:
  330. case PIX_FMT_BGR24:
  331. rgb_plane = s->current_frame.data[0];
  332. stride = s->current_frame.linesize[0];
  333. line_inc = stride - width * 3;
  334. index = y * stride + x * 3;
  335. current_x = x;
  336. while(pixel_count--) {
  337. pix = *pixel_buffer++;
  338. rgb_plane[index++] = s->palette[pix * 4 + 0];
  339. rgb_plane[index++] = s->palette[pix * 4 + 1];
  340. rgb_plane[index++] = s->palette[pix * 4 + 2];
  341. ADVANCE_CURRENT_X();
  342. }
  343. break;
  344. case PIX_FMT_RGBA32:
  345. rgb32_plane = (unsigned int *)s->current_frame.data[0];
  346. palette32 = (unsigned int *)s->palette;
  347. stride = s->current_frame.linesize[0] / 4;
  348. line_inc = stride - width;
  349. index = y * stride + x;
  350. current_x = x;
  351. while(pixel_count--) {
  352. rgb32_plane[index++] = palette32[*pixel_buffer++];
  353. ADVANCE_CURRENT_X();
  354. }
  355. break;
  356. case PIX_FMT_YUV444P:
  357. y_plane = s->current_frame.data[0];
  358. u_plane = s->current_frame.data[1];
  359. v_plane = s->current_frame.data[2];
  360. stride = s->current_frame.linesize[0];
  361. line_inc = stride - width;
  362. index = y * stride + x;
  363. current_x = x;
  364. while(pixel_count--) {
  365. pix = *pixel_buffer++;
  366. y_plane[index] = s->palette[pix * 4 + 0];
  367. u_plane[index] = s->palette[pix * 4 + 1];
  368. v_plane[index] = s->palette[pix * 4 + 2];
  369. index++;
  370. ADVANCE_CURRENT_X();
  371. }
  372. break;
  373. default:
  374. av_log(s->avctx, AV_LOG_ERROR, " Xan WC3: Unhandled colorspace\n");
  375. break;
  376. }
  377. }
  378. #define ADVANCE_CURFRAME_X() \
  379. curframe_x++; \
  380. if (curframe_x >= width) { \
  381. curframe_index += line_inc; \
  382. curframe_x = 0; \
  383. }
  384. #define ADVANCE_PREVFRAME_X() \
  385. prevframe_x++; \
  386. if (prevframe_x >= width) { \
  387. prevframe_index += line_inc; \
  388. prevframe_x = 0; \
  389. }
  390. static void inline xan_wc3_copy_pixel_run(XanContext *s,
  391. int x, int y, int pixel_count, int motion_x, int motion_y)
  392. {
  393. int stride;
  394. int line_inc;
  395. int curframe_index, prevframe_index;
  396. int curframe_x, prevframe_x;
  397. int width = s->avctx->width;
  398. unsigned char *palette_plane, *prev_palette_plane;
  399. unsigned char *y_plane, *u_plane, *v_plane;
  400. unsigned char *prev_y_plane, *prev_u_plane, *prev_v_plane;
  401. unsigned char *rgb_plane, *prev_rgb_plane;
  402. unsigned short *rgb16_plane, *prev_rgb16_plane;
  403. unsigned int *rgb32_plane, *prev_rgb32_plane;
  404. switch (s->avctx->pix_fmt) {
  405. case PIX_FMT_PAL8:
  406. palette_plane = s->current_frame.data[0];
  407. prev_palette_plane = s->last_frame.data[0];
  408. stride = s->current_frame.linesize[0];
  409. line_inc = stride - width;
  410. curframe_index = y * stride + x;
  411. curframe_x = x;
  412. prevframe_index = (y + motion_y) * stride + x + motion_x;
  413. prevframe_x = x + motion_x;
  414. while(pixel_count--) {
  415. palette_plane[curframe_index++] =
  416. prev_palette_plane[prevframe_index++];
  417. ADVANCE_CURFRAME_X();
  418. ADVANCE_PREVFRAME_X();
  419. }
  420. break;
  421. case PIX_FMT_RGB555:
  422. case PIX_FMT_RGB565:
  423. rgb16_plane = (unsigned short *)s->current_frame.data[0];
  424. prev_rgb16_plane = (unsigned short *)s->last_frame.data[0];
  425. stride = s->current_frame.linesize[0] / 2;
  426. line_inc = stride - width;
  427. curframe_index = y * stride + x;
  428. curframe_x = x;
  429. prevframe_index = (y + motion_y) * stride + x + motion_x;
  430. prevframe_x = x + motion_x;
  431. while(pixel_count--) {
  432. rgb16_plane[curframe_index++] =
  433. prev_rgb16_plane[prevframe_index++];
  434. ADVANCE_CURFRAME_X();
  435. ADVANCE_PREVFRAME_X();
  436. }
  437. break;
  438. case PIX_FMT_RGB24:
  439. case PIX_FMT_BGR24:
  440. rgb_plane = s->current_frame.data[0];
  441. prev_rgb_plane = s->last_frame.data[0];
  442. stride = s->current_frame.linesize[0];
  443. line_inc = stride - width * 3;
  444. curframe_index = y * stride + x * 3;
  445. curframe_x = x;
  446. prevframe_index = (y + motion_y) * stride +
  447. (3 * (x + motion_x));
  448. prevframe_x = x + motion_x;
  449. while(pixel_count--) {
  450. rgb_plane[curframe_index++] = prev_rgb_plane[prevframe_index++];
  451. rgb_plane[curframe_index++] = prev_rgb_plane[prevframe_index++];
  452. rgb_plane[curframe_index++] = prev_rgb_plane[prevframe_index++];
  453. ADVANCE_CURFRAME_X();
  454. ADVANCE_PREVFRAME_X();
  455. }
  456. break;
  457. case PIX_FMT_RGBA32:
  458. rgb32_plane = (unsigned int *)s->current_frame.data[0];
  459. prev_rgb32_plane = (unsigned int *)s->last_frame.data[0];
  460. stride = s->current_frame.linesize[0] / 4;
  461. line_inc = stride - width;
  462. curframe_index = y * stride + x;
  463. curframe_x = x;
  464. prevframe_index = (y + motion_y) * stride + x + motion_x;
  465. prevframe_x = x + motion_x;
  466. while(pixel_count--) {
  467. rgb32_plane[curframe_index++] =
  468. prev_rgb32_plane[prevframe_index++];
  469. ADVANCE_CURFRAME_X();
  470. ADVANCE_PREVFRAME_X();
  471. }
  472. break;
  473. case PIX_FMT_YUV444P:
  474. y_plane = s->current_frame.data[0];
  475. u_plane = s->current_frame.data[1];
  476. v_plane = s->current_frame.data[2];
  477. prev_y_plane = s->last_frame.data[0];
  478. prev_u_plane = s->last_frame.data[1];
  479. prev_v_plane = s->last_frame.data[2];
  480. stride = s->current_frame.linesize[0];
  481. line_inc = stride - width;
  482. curframe_index = y * stride + x;
  483. curframe_x = x;
  484. prevframe_index = (y + motion_y) * stride + x + motion_x;
  485. prevframe_x = x + motion_x;
  486. while(pixel_count--) {
  487. y_plane[curframe_index] = prev_y_plane[prevframe_index];
  488. u_plane[curframe_index] = prev_u_plane[prevframe_index];
  489. v_plane[curframe_index] = prev_v_plane[prevframe_index];
  490. curframe_index++;
  491. ADVANCE_CURFRAME_X();
  492. prevframe_index++;
  493. ADVANCE_PREVFRAME_X();
  494. }
  495. break;
  496. default:
  497. av_log(s->avctx, AV_LOG_ERROR, " Xan WC3: Unhandled colorspace\n");
  498. break;
  499. }
  500. }
  501. static void xan_wc3_decode_frame(XanContext *s) {
  502. int width = s->avctx->width;
  503. int height = s->avctx->height;
  504. int total_pixels = width * height;
  505. unsigned char opcode;
  506. unsigned char flag = 0;
  507. int size = 0;
  508. int motion_x, motion_y;
  509. int x, y;
  510. unsigned char *opcode_buffer = s->buffer1;
  511. unsigned char *imagedata_buffer = s->buffer2;
  512. /* pointers to segments inside the compressed chunk */
  513. unsigned char *huffman_segment;
  514. unsigned char *size_segment;
  515. unsigned char *vector_segment;
  516. unsigned char *imagedata_segment;
  517. huffman_segment = s->buf + LE_16(&s->buf[0]);
  518. size_segment = s->buf + LE_16(&s->buf[2]);
  519. vector_segment = s->buf + LE_16(&s->buf[4]);
  520. imagedata_segment = s->buf + LE_16(&s->buf[6]);
  521. xan_huffman_decode(opcode_buffer, huffman_segment);
  522. if (imagedata_segment[0] == 2)
  523. xan_unpack(imagedata_buffer, &imagedata_segment[1]);
  524. else
  525. imagedata_buffer = &imagedata_segment[1];
  526. /* use the decoded data segments to build the frame */
  527. x = y = 0;
  528. while (total_pixels) {
  529. opcode = *opcode_buffer++;
  530. size = 0;
  531. switch (opcode) {
  532. case 0:
  533. flag ^= 1;
  534. continue;
  535. case 1:
  536. case 2:
  537. case 3:
  538. case 4:
  539. case 5:
  540. case 6:
  541. case 7:
  542. case 8:
  543. size = opcode;
  544. break;
  545. case 12:
  546. case 13:
  547. case 14:
  548. case 15:
  549. case 16:
  550. case 17:
  551. case 18:
  552. size += (opcode - 10);
  553. break;
  554. case 9:
  555. case 19:
  556. size = *size_segment++;
  557. break;
  558. case 10:
  559. case 20:
  560. size = BE_16(&size_segment[0]);
  561. size_segment += 2;
  562. break;
  563. case 11:
  564. case 21:
  565. size = (size_segment[0] << 16) | (size_segment[1] << 8) |
  566. size_segment[2];
  567. size_segment += 3;
  568. break;
  569. }
  570. if (opcode < 12) {
  571. flag ^= 1;
  572. if (flag) {
  573. /* run of (size) pixels is unchanged from last frame */
  574. xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
  575. } else {
  576. /* output a run of pixels from imagedata_buffer */
  577. xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);
  578. imagedata_buffer += size;
  579. }
  580. } else {
  581. /* run-based motion compensation from last frame */
  582. motion_x = (*vector_segment >> 4) & 0xF;
  583. motion_y = *vector_segment & 0xF;
  584. vector_segment++;
  585. /* sign extension */
  586. if (motion_x & 0x8)
  587. motion_x |= 0xFFFFFFF0;
  588. if (motion_y & 0x8)
  589. motion_y |= 0xFFFFFFF0;
  590. /* copy a run of pixels from the previous frame */
  591. xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
  592. flag = 0;
  593. }
  594. /* coordinate accounting */
  595. total_pixels -= size;
  596. while (size) {
  597. if (x + size >= width) {
  598. y++;
  599. size -= (width - x);
  600. x = 0;
  601. } else {
  602. x += size;
  603. size = 0;
  604. }
  605. }
  606. }
  607. /* for PAL8, make the palette available on the way out */
  608. if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
  609. memcpy(s->current_frame.data[1], s->palette, PALETTE_COUNT * 4);
  610. s->current_frame.palette_has_changed = 1;
  611. s->avctx->palctrl->palette_changed = 0;
  612. }
  613. }
  614. static void xan_wc4_decode_frame(XanContext *s) {
  615. }
  616. static int xan_decode_frame(AVCodecContext *avctx,
  617. void *data, int *data_size,
  618. uint8_t *buf, int buf_size)
  619. {
  620. XanContext *s = avctx->priv_data;
  621. AVPaletteControl *palette_control = avctx->palctrl;
  622. int keyframe = 0;
  623. if (palette_control->palette_changed) {
  624. /* load the new palette and reset the palette control */
  625. xan_wc3_build_palette(s, palette_control->palette);
  626. /* If pal8 we clear flag when we copy palette */
  627. if (s->avctx->pix_fmt != PIX_FMT_PAL8)
  628. palette_control->palette_changed = 0;
  629. keyframe = 1;
  630. }
  631. if (avctx->get_buffer(avctx, &s->current_frame)) {
  632. av_log(s->avctx, AV_LOG_ERROR, " Xan Video: get_buffer() failed\n");
  633. return -1;
  634. }
  635. s->current_frame.reference = 3;
  636. s->buf = buf;
  637. s->size = buf_size;
  638. if (avctx->codec->id == CODEC_ID_XAN_WC3)
  639. xan_wc3_decode_frame(s);
  640. else if (avctx->codec->id == CODEC_ID_XAN_WC4)
  641. xan_wc4_decode_frame(s);
  642. /* release the last frame if it is allocated */
  643. if (s->last_frame.data[0])
  644. avctx->release_buffer(avctx, &s->last_frame);
  645. /* shuffle frames */
  646. s->last_frame = s->current_frame;
  647. *data_size = sizeof(AVFrame);
  648. *(AVFrame*)data = s->current_frame;
  649. /* always report that the buffer was completely consumed */
  650. return buf_size;
  651. }
  652. static int xan_decode_end(AVCodecContext *avctx)
  653. {
  654. XanContext *s = avctx->priv_data;
  655. /* release the last frame */
  656. avctx->release_buffer(avctx, &s->last_frame);
  657. av_free(s->buffer1);
  658. av_free(s->buffer2);
  659. return 0;
  660. }
  661. AVCodec xan_wc3_decoder = {
  662. "xan_wc3",
  663. CODEC_TYPE_VIDEO,
  664. CODEC_ID_XAN_WC3,
  665. sizeof(XanContext),
  666. xan_decode_init,
  667. NULL,
  668. xan_decode_end,
  669. xan_decode_frame,
  670. CODEC_CAP_DR1,
  671. };
  672. /*
  673. AVCodec xan_wc4_decoder = {
  674. "xan_wc4",
  675. CODEC_TYPE_VIDEO,
  676. CODEC_ID_XAN_WC4,
  677. sizeof(XanContext),
  678. xan_decode_init,
  679. NULL,
  680. xan_decode_end,
  681. xan_decode_frame,
  682. CODEC_CAP_DR1,
  683. };
  684. */