2 * Wing Commander/Xan Video Decoder
3 * Copyright (C) 2003 the ffmpeg project
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.
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.
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
23 * Xan video decoder for Wing Commander III & IV computer games
24 * by Mario Brito (mbrito@student.dei.uc.pt)
25 * and Mike Melanson (melanson@pcisys.net)
27 * The xan_wc3 decoder outputs the following colorspaces natively:
28 * PAL8 (default), RGB555, RGB565, RGB24, BGR24, RGBA32, YUV444P
40 #define PALETTE_COUNT 256
41 #define PALETTE_CONTROL_SIZE ((256 * 3) + 1)
43 typedef struct XanContext {
45 AVCodecContext *avctx;
48 AVFrame current_frame;
53 unsigned char palette[PALETTE_COUNT * 4];
56 unsigned char *buffer1;
57 unsigned char *buffer2;
61 /* RGB -> YUV conversion stuff */
62 #define SCALEFACTOR 65536
63 #define CENTERSAMPLE 128
65 #define COMPUTE_Y(r, g, b) \
67 ((y_r_table[r] + y_g_table[g] + y_b_table[b]) / SCALEFACTOR)
68 #define COMPUTE_U(r, g, b) \
70 ((u_r_table[r] + u_g_table[g] + u_b_table[b]) / SCALEFACTOR + CENTERSAMPLE)
71 #define COMPUTE_V(r, g, b) \
73 ((v_r_table[r] + v_g_table[g] + v_b_table[b]) / SCALEFACTOR + CENTERSAMPLE)
75 #define Y_R (SCALEFACTOR * 0.29900)
76 #define Y_G (SCALEFACTOR * 0.58700)
77 #define Y_B (SCALEFACTOR * 0.11400)
79 #define U_R (SCALEFACTOR * -0.16874)
80 #define U_G (SCALEFACTOR * -0.33126)
81 #define U_B (SCALEFACTOR * 0.50000)
83 #define V_R (SCALEFACTOR * 0.50000)
84 #define V_G (SCALEFACTOR * -0.41869)
85 #define V_B (SCALEFACTOR * -0.08131)
88 * Precalculate all of the YUV tables since it requires fewer than
89 * 10 kilobytes to store them.
91 static int y_r_table[256];
92 static int y_g_table[256];
93 static int y_b_table[256];
95 static int u_r_table[256];
96 static int u_g_table[256];
97 static int u_b_table[256];
99 static int v_r_table[256];
100 static int v_g_table[256];
101 static int v_b_table[256];
103 static int xan_decode_init(AVCodecContext *avctx)
105 XanContext *s = avctx->priv_data;
110 if ((avctx->codec->id == CODEC_ID_XAN_WC3) &&
111 (s->avctx->palctrl == NULL)) {
112 av_log(avctx, AV_LOG_ERROR, " WC3 Xan video: palette expected.\n");
116 avctx->pix_fmt = PIX_FMT_PAL8;
117 avctx->has_b_frames = 0;
118 dsputil_init(&s->dsp, avctx);
120 /* initialize the RGB -> YUV tables */
121 for (i = 0; i < 256; i++) {
122 y_r_table[i] = Y_R * i;
123 y_g_table[i] = Y_G * i;
124 y_b_table[i] = Y_B * i;
126 u_r_table[i] = U_R * i;
127 u_g_table[i] = U_G * i;
128 u_b_table[i] = U_B * i;
130 v_r_table[i] = V_R * i;
131 v_g_table[i] = V_G * i;
132 v_b_table[i] = V_B * i;
135 s->buffer1 = av_malloc(avctx->width * avctx->height);
136 s->buffer2 = av_malloc(avctx->width * avctx->height);
137 if (!s->buffer1 || !s->buffer2)
143 /* This function is used in lieu of memcpy(). This decoder can not use
144 * memcpy because the memory locations often overlap and
145 * memcpy doesn't like that; it's not uncommon, for example, for
146 * dest = src+1, to turn byte A into pattern AAAAAAAA.
147 * This was originally repz movsb in Intel x86 ASM. */
148 static inline void bytecopy(unsigned char *dest, unsigned char *src, int count)
152 for (i = 0; i < count; i++)
156 static int xan_huffman_decode(unsigned char *dest, unsigned char *src)
158 unsigned char byte = *src++;
159 unsigned char ival = byte + 0x16;
160 unsigned char * ptr = src + byte*2;
161 unsigned char val = ival;
164 unsigned char bits = *ptr++;
166 while ( val != 0x16 ) {
167 if ( (1 << counter) & bits )
168 val = src[byte + val - 0x17];
170 val = src[val - 0x17];
177 if (counter++ == 7) {
186 static void xan_unpack(unsigned char *dest, unsigned char *src)
188 unsigned char opcode;
191 int byte1, byte2, byte3;
196 if ( (opcode & 0x80) == 0 ) {
201 bytecopy(dest, src, size); dest += size; src += size;
203 size = ((opcode & 0x1c) >> 2) + 3;
204 bytecopy (dest, dest - (((opcode & 0x60) << 3) + offset + 1), size);
207 } else if ( (opcode & 0x40) == 0 ) {
213 bytecopy (dest, src, size); dest += size; src += size;
215 size = (opcode & 0x3f) + 4;
216 bytecopy (dest, dest - (((byte1 & 0x3f) << 8) + byte2 + 1), size);
219 } else if ( (opcode & 0x20) == 0 ) {
226 bytecopy (dest, src, size); dest += size; src += size;
228 size = byte3 + 5 + ((opcode & 0xc) << 6);
230 dest - ((((opcode & 0x10) >> 4) << 0x10) + 1 + (byte1 << 8) + byte2),
234 size = ((opcode & 0x1f) << 2) + 4;
239 bytecopy (dest, src, size); dest += size; src += size;
244 bytecopy(dest, src, size); dest += size; src += size;
247 static void inline xan_wc3_build_palette(XanContext *s,
248 unsigned int *palette_data)
251 unsigned char r, g, b;
252 unsigned short *palette16;
253 unsigned int *palette32;
254 unsigned int pal_elem;
256 /* transform the palette passed through the palette control structure
257 * into the necessary internal format depending on colorspace */
259 switch (s->avctx->pix_fmt) {
262 palette16 = (unsigned short *)s->palette;
263 for (i = 0; i < PALETTE_COUNT; i++) {
264 pal_elem = palette_data[i];
265 r = (pal_elem >> 16) & 0xff;
266 g = (pal_elem >> 8) & 0xff;
276 palette16 = (unsigned short *)s->palette;
277 for (i = 0; i < PALETTE_COUNT; i++) {
278 pal_elem = palette_data[i];
279 r = (pal_elem >> 16) & 0xff;
280 g = (pal_elem >> 8) & 0xff;
290 for (i = 0; i < PALETTE_COUNT; i++) {
291 pal_elem = palette_data[i];
292 r = (pal_elem >> 16) & 0xff;
293 g = (pal_elem >> 8) & 0xff;
295 s->palette[i * 4 + 0] = r;
296 s->palette[i * 4 + 1] = g;
297 s->palette[i * 4 + 2] = b;
302 for (i = 0; i < PALETTE_COUNT; i++) {
303 pal_elem = palette_data[i];
304 r = (pal_elem >> 16) & 0xff;
305 g = (pal_elem >> 8) & 0xff;
307 s->palette[i * 4 + 0] = b;
308 s->palette[i * 4 + 1] = g;
309 s->palette[i * 4 + 2] = r;
315 palette32 = (unsigned int *)s->palette;
316 memcpy (palette32, palette_data, PALETTE_COUNT * sizeof(unsigned int));
319 case PIX_FMT_YUV444P:
320 for (i = 0; i < PALETTE_COUNT; i++) {
321 pal_elem = palette_data[i];
322 r = (pal_elem >> 16) & 0xff;
323 g = (pal_elem >> 8) & 0xff;
325 s->palette[i * 4 + 0] = COMPUTE_Y(r, g, b);
326 s->palette[i * 4 + 1] = COMPUTE_U(r, g, b);
327 s->palette[i * 4 + 2] = COMPUTE_V(r, g, b);
332 av_log(s->avctx, AV_LOG_ERROR, " Xan WC3: Unhandled colorspace\n");
337 /* advance current_x variable; reset accounting variables if current_x
338 * moves beyond width */
339 #define ADVANCE_CURRENT_X() \
341 if (current_x >= width) { \
346 static void inline xan_wc3_output_pixel_run(XanContext *s,
347 unsigned char *pixel_buffer, int x, int y, int pixel_count)
353 int width = s->avctx->width;
355 unsigned char *palette_plane;
356 unsigned char *y_plane;
357 unsigned char *u_plane;
358 unsigned char *v_plane;
359 unsigned char *rgb_plane;
360 unsigned short *rgb16_plane;
361 unsigned short *palette16;
362 unsigned int *rgb32_plane;
363 unsigned int *palette32;
365 switch (s->avctx->pix_fmt) {
368 palette_plane = s->current_frame.data[0];
369 stride = s->current_frame.linesize[0];
370 line_inc = stride - width;
371 index = y * stride + x;
373 while(pixel_count--) {
375 /* don't do a memcpy() here; keyframes generally copy an entire
376 * frame of data and the stride needs to be accounted for */
377 palette_plane[index++] = *pixel_buffer++;
385 rgb16_plane = (unsigned short *)s->current_frame.data[0];
386 palette16 = (unsigned short *)s->palette;
387 stride = s->current_frame.linesize[0] / 2;
388 line_inc = stride - width;
389 index = y * stride + x;
391 while(pixel_count--) {
393 rgb16_plane[index++] = palette16[*pixel_buffer++];
401 rgb_plane = s->current_frame.data[0];
402 stride = s->current_frame.linesize[0];
403 line_inc = stride - width * 3;
404 index = y * stride + x * 3;
406 while(pixel_count--) {
407 pix = *pixel_buffer++;
409 rgb_plane[index++] = s->palette[pix * 4 + 0];
410 rgb_plane[index++] = s->palette[pix * 4 + 1];
411 rgb_plane[index++] = s->palette[pix * 4 + 2];
418 rgb32_plane = (unsigned int *)s->current_frame.data[0];
419 palette32 = (unsigned int *)s->palette;
420 stride = s->current_frame.linesize[0] / 4;
421 line_inc = stride - width;
422 index = y * stride + x;
424 while(pixel_count--) {
426 rgb32_plane[index++] = palette32[*pixel_buffer++];
432 case PIX_FMT_YUV444P:
433 y_plane = s->current_frame.data[0];
434 u_plane = s->current_frame.data[1];
435 v_plane = s->current_frame.data[2];
436 stride = s->current_frame.linesize[0];
437 line_inc = stride - width;
438 index = y * stride + x;
440 while(pixel_count--) {
441 pix = *pixel_buffer++;
443 y_plane[index] = s->palette[pix * 4 + 0];
444 u_plane[index] = s->palette[pix * 4 + 1];
445 v_plane[index] = s->palette[pix * 4 + 2];
453 av_log(s->avctx, AV_LOG_ERROR, " Xan WC3: Unhandled colorspace\n");
458 #define ADVANCE_CURFRAME_X() \
460 if (curframe_x >= width) { \
461 curframe_index += line_inc; \
465 #define ADVANCE_PREVFRAME_X() \
467 if (prevframe_x >= width) { \
468 prevframe_index += line_inc; \
472 static void inline xan_wc3_copy_pixel_run(XanContext *s,
473 int x, int y, int pixel_count, int motion_x, int motion_y)
477 int curframe_index, prevframe_index;
478 int curframe_x, prevframe_x;
479 int width = s->avctx->width;
480 unsigned char *palette_plane, *prev_palette_plane;
481 unsigned char *y_plane, *u_plane, *v_plane;
482 unsigned char *prev_y_plane, *prev_u_plane, *prev_v_plane;
483 unsigned char *rgb_plane, *prev_rgb_plane;
484 unsigned short *rgb16_plane, *prev_rgb16_plane;
485 unsigned int *rgb32_plane, *prev_rgb32_plane;
487 switch (s->avctx->pix_fmt) {
490 palette_plane = s->current_frame.data[0];
491 prev_palette_plane = s->last_frame.data[0];
492 stride = s->current_frame.linesize[0];
493 line_inc = stride - width;
494 curframe_index = y * stride + x;
496 prevframe_index = (y + motion_y) * stride + x + motion_x;
497 prevframe_x = x + motion_x;
498 while(pixel_count--) {
500 palette_plane[curframe_index++] =
501 prev_palette_plane[prevframe_index++];
503 ADVANCE_CURFRAME_X();
504 ADVANCE_PREVFRAME_X();
510 rgb16_plane = (unsigned short *)s->current_frame.data[0];
511 prev_rgb16_plane = (unsigned short *)s->last_frame.data[0];
512 stride = s->current_frame.linesize[0] / 2;
513 line_inc = stride - width;
514 curframe_index = y * stride + x;
516 prevframe_index = (y + motion_y) * stride + x + motion_x;
517 prevframe_x = x + motion_x;
518 while(pixel_count--) {
520 rgb16_plane[curframe_index++] =
521 prev_rgb16_plane[prevframe_index++];
523 ADVANCE_CURFRAME_X();
524 ADVANCE_PREVFRAME_X();
530 rgb_plane = s->current_frame.data[0];
531 prev_rgb_plane = s->last_frame.data[0];
532 stride = s->current_frame.linesize[0];
533 line_inc = stride - width * 3;
534 curframe_index = y * stride + x * 3;
536 prevframe_index = (y + motion_y) * stride +
537 (3 * (x + motion_x));
538 prevframe_x = x + motion_x;
539 while(pixel_count--) {
541 rgb_plane[curframe_index++] = prev_rgb_plane[prevframe_index++];
542 rgb_plane[curframe_index++] = prev_rgb_plane[prevframe_index++];
543 rgb_plane[curframe_index++] = prev_rgb_plane[prevframe_index++];
545 ADVANCE_CURFRAME_X();
546 ADVANCE_PREVFRAME_X();
551 rgb32_plane = (unsigned int *)s->current_frame.data[0];
552 prev_rgb32_plane = (unsigned int *)s->last_frame.data[0];
553 stride = s->current_frame.linesize[0] / 4;
554 line_inc = stride - width;
555 curframe_index = y * stride + x;
557 prevframe_index = (y + motion_y) * stride + x + motion_x;
558 prevframe_x = x + motion_x;
559 while(pixel_count--) {
561 rgb32_plane[curframe_index++] =
562 prev_rgb32_plane[prevframe_index++];
564 ADVANCE_CURFRAME_X();
565 ADVANCE_PREVFRAME_X();
569 case PIX_FMT_YUV444P:
570 y_plane = s->current_frame.data[0];
571 u_plane = s->current_frame.data[1];
572 v_plane = s->current_frame.data[2];
573 prev_y_plane = s->last_frame.data[0];
574 prev_u_plane = s->last_frame.data[1];
575 prev_v_plane = s->last_frame.data[2];
576 stride = s->current_frame.linesize[0];
577 line_inc = stride - width;
578 curframe_index = y * stride + x;
580 prevframe_index = (y + motion_y) * stride + x + motion_x;
581 prevframe_x = x + motion_x;
582 while(pixel_count--) {
584 y_plane[curframe_index] = prev_y_plane[prevframe_index];
585 u_plane[curframe_index] = prev_u_plane[prevframe_index];
586 v_plane[curframe_index] = prev_v_plane[prevframe_index];
589 ADVANCE_CURFRAME_X();
591 ADVANCE_PREVFRAME_X();
596 av_log(s->avctx, AV_LOG_ERROR, " Xan WC3: Unhandled colorspace\n");
601 static void xan_wc3_decode_frame(XanContext *s) {
603 int width = s->avctx->width;
604 int height = s->avctx->height;
605 int total_pixels = width * height;
606 unsigned char opcode;
607 unsigned char flag = 0;
609 int motion_x, motion_y;
612 unsigned char *opcode_buffer = s->buffer1;
613 unsigned char *imagedata_buffer = s->buffer2;
615 /* pointers to segments inside the compressed chunk */
616 unsigned char *huffman_segment;
617 unsigned char *size_segment;
618 unsigned char *vector_segment;
619 unsigned char *imagedata_segment;
621 huffman_segment = s->buf + LE_16(&s->buf[0]);
622 size_segment = s->buf + LE_16(&s->buf[2]);
623 vector_segment = s->buf + LE_16(&s->buf[4]);
624 imagedata_segment = s->buf + LE_16(&s->buf[6]);
626 xan_huffman_decode(opcode_buffer, huffman_segment);
628 if (imagedata_segment[0] == 2)
629 xan_unpack(imagedata_buffer, &imagedata_segment[1]);
631 imagedata_buffer = &imagedata_segment[1];
633 /* use the decoded data segments to build the frame */
635 while (total_pixels) {
637 opcode = *opcode_buffer++;
664 size += (opcode - 10);
669 size = *size_segment++;
674 size = BE_16(&size_segment[0]);
680 size = (size_segment[0] << 16) | (size_segment[1] << 8) |
689 /* run of (size) pixels is unchanged from last frame */
690 xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
692 /* output a run of pixels from imagedata_buffer */
693 xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);
694 imagedata_buffer += size;
697 /* run-based motion compensation from last frame */
698 motion_x = (*vector_segment >> 4) & 0xF;
699 motion_y = *vector_segment & 0xF;
704 motion_x |= 0xFFFFFFF0;
706 motion_y |= 0xFFFFFFF0;
708 /* copy a run of pixels from the previous frame */
709 xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
714 /* coordinate accounting */
715 total_pixels -= size;
717 if (x + size >= width) {
728 /* for PAL8, make the palette available on the way out */
729 if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
730 memcpy(s->current_frame.data[1], s->palette, PALETTE_COUNT * 4);
731 s->current_frame.palette_has_changed = 1;
732 s->avctx->palctrl->palette_changed = 0;
736 static void xan_wc4_decode_frame(XanContext *s) {
739 static int xan_decode_frame(AVCodecContext *avctx,
740 void *data, int *data_size,
741 uint8_t *buf, int buf_size)
743 XanContext *s = avctx->priv_data;
744 AVPaletteControl *palette_control = avctx->palctrl;
747 if (palette_control->palette_changed) {
748 /* load the new palette and reset the palette control */
749 xan_wc3_build_palette(s, palette_control->palette);
750 /* If pal8 we clear flag when we copy palette */
751 if (s->avctx->pix_fmt != PIX_FMT_PAL8)
752 palette_control->palette_changed = 0;
756 if (avctx->get_buffer(avctx, &s->current_frame)) {
757 av_log(s->avctx, AV_LOG_ERROR, " Xan Video: get_buffer() failed\n");
760 s->current_frame.reference = 3;
765 if (avctx->codec->id == CODEC_ID_XAN_WC3)
766 xan_wc3_decode_frame(s);
767 else if (avctx->codec->id == CODEC_ID_XAN_WC4)
768 xan_wc4_decode_frame(s);
770 /* release the last frame if it is allocated */
771 if (s->last_frame.data[0])
772 avctx->release_buffer(avctx, &s->last_frame);
775 s->last_frame = s->current_frame;
777 *data_size = sizeof(AVFrame);
778 *(AVFrame*)data = s->current_frame;
780 /* always report that the buffer was completely consumed */
784 static int xan_decode_end(AVCodecContext *avctx)
786 XanContext *s = avctx->priv_data;
788 /* release the last frame */
789 avctx->release_buffer(avctx, &s->last_frame);
797 AVCodec xan_wc3_decoder = {
810 AVCodec xan_wc4_decoder = {