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 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
62 #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
63 #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
64 (((uint8_t*)(x))[2] << 16) | \
65 (((uint8_t*)(x))[1] << 8) | \
68 /* RGB -> YUV conversion stuff */
69 #define SCALEFACTOR 65536
70 #define CENTERSAMPLE 128
72 #define COMPUTE_Y(r, g, b) \
74 ((y_r_table[r] + y_g_table[g] + y_b_table[b]) / SCALEFACTOR)
75 #define COMPUTE_U(r, g, b) \
77 ((u_r_table[r] + u_g_table[g] + u_b_table[b]) / SCALEFACTOR + CENTERSAMPLE)
78 #define COMPUTE_V(r, g, b) \
80 ((v_r_table[r] + v_g_table[g] + v_b_table[b]) / SCALEFACTOR + CENTERSAMPLE)
82 #define Y_R (SCALEFACTOR * 0.29900)
83 #define Y_G (SCALEFACTOR * 0.58700)
84 #define Y_B (SCALEFACTOR * 0.11400)
86 #define U_R (SCALEFACTOR * -0.16874)
87 #define U_G (SCALEFACTOR * -0.33126)
88 #define U_B (SCALEFACTOR * 0.50000)
90 #define V_R (SCALEFACTOR * 0.50000)
91 #define V_G (SCALEFACTOR * -0.41869)
92 #define V_B (SCALEFACTOR * -0.08131)
95 * Precalculate all of the YUV tables since it requires fewer than
96 * 10 kilobytes to store them.
98 static int y_r_table
[256];
99 static int y_g_table
[256];
100 static int y_b_table
[256];
102 static int u_r_table
[256];
103 static int u_g_table
[256];
104 static int u_b_table
[256];
106 static int v_r_table
[256];
107 static int v_g_table
[256];
108 static int v_b_table
[256];
110 static int xan_decode_init(AVCodecContext
*avctx
)
112 XanContext
*s
= avctx
->priv_data
;
117 if ((avctx
->codec
->id
== CODEC_ID_XAN_WC3
) &&
118 (s
->avctx
->palctrl
== NULL
)) {
119 printf (" WC3 Xan video: palette expected.\n");
123 avctx
->pix_fmt
= PIX_FMT_PAL8
;
124 avctx
->has_b_frames
= 0;
125 dsputil_init(&s
->dsp
, avctx
);
127 /* initialize the RGB -> YUV tables */
128 for (i
= 0; i
< 256; i
++) {
129 y_r_table
[i
] = Y_R
* i
;
130 y_g_table
[i
] = Y_G
* i
;
131 y_b_table
[i
] = Y_B
* i
;
133 u_r_table
[i
] = U_R
* i
;
134 u_g_table
[i
] = U_G
* i
;
135 u_b_table
[i
] = U_B
* i
;
137 v_r_table
[i
] = V_R
* i
;
138 v_g_table
[i
] = V_G
* i
;
139 v_b_table
[i
] = V_B
* i
;
142 s
->buffer1
= av_malloc(avctx
->width
* avctx
->height
);
143 s
->buffer2
= av_malloc(avctx
->width
* avctx
->height
);
144 if (!s
->buffer1
|| !s
->buffer2
)
150 /* This function is used in lieu of memcpy(). This decoder can not use
151 * memcpy because the memory locations often overlap and
152 * memcpy doesn't like that; it's not uncommon, for example, for
153 * dest = src+1, to turn byte A into pattern AAAAAAAA.
154 * This was originally repz movsb in Intel x86 ASM. */
155 static inline void bytecopy(unsigned char *dest
, unsigned char *src
, int count
)
159 for (i
= 0; i
< count
; i
++)
163 static int xan_huffman_decode(unsigned char *dest
, unsigned char *src
)
165 unsigned char byte
= *src
++;
166 unsigned char ival
= byte
+ 0x16;
167 unsigned char * ptr
= src
+ byte
*2;
168 unsigned char val
= ival
;
171 unsigned char bits
= *ptr
++;
173 while ( val
!= 0x16 ) {
174 if ( (1 << counter
) & bits
)
175 val
= src
[byte
+ val
- 0x17];
177 val
= src
[val
- 0x17];
184 if (counter
++ == 7) {
193 static void xan_unpack(unsigned char *dest
, unsigned char *src
)
195 unsigned char opcode
;
198 int byte1
, byte2
, byte3
;
203 if ( (opcode
& 0x80) == 0 ) {
208 bytecopy(dest
, src
, size
); dest
+= size
; src
+= size
;
210 size
= ((opcode
& 0x1c) >> 2) + 3;
211 bytecopy (dest
, dest
- (((opcode
& 0x60) << 3) + offset
+ 1), size
);
214 } else if ( (opcode
& 0x40) == 0 ) {
220 bytecopy (dest
, src
, size
); dest
+= size
; src
+= size
;
222 size
= (opcode
& 0x3f) + 4;
223 bytecopy (dest
, dest
- (((byte1
& 0x3f) << 8) + byte2
+ 1), size
);
226 } else if ( (opcode
& 0x20) == 0 ) {
233 bytecopy (dest
, src
, size
); dest
+= size
; src
+= size
;
235 size
= byte3
+ 5 + ((opcode
& 0xc) << 6);
237 dest
- ((((opcode
& 0x10) >> 4) << 0x10) + 1 + (byte1
<< 8) + byte2
),
241 size
= ((opcode
& 0x1f) << 2) + 4;
246 bytecopy (dest
, src
, size
); dest
+= size
; src
+= size
;
251 bytecopy(dest
, src
, size
); dest
+= size
; src
+= size
;
254 static void inline xan_wc3_build_palette(XanContext
*s
,
255 unsigned int *palette_data
)
258 unsigned char r
, g
, b
;
259 unsigned short *palette16
;
260 unsigned int *palette32
;
261 unsigned int pal_elem
;
263 /* transform the palette passed through the palette control structure
264 * into the necessary internal format depending on colorspace */
266 switch (s
->avctx
->pix_fmt
) {
269 palette16
= (unsigned short *)s
->palette
;
270 for (i
= 0; i
< PALETTE_COUNT
; i
++) {
271 pal_elem
= palette_data
[i
];
272 r
= (pal_elem
>> 16) & 0xff;
273 g
= (pal_elem
>> 8) & 0xff;
283 palette16
= (unsigned short *)s
->palette
;
284 for (i
= 0; i
< PALETTE_COUNT
; i
++) {
285 pal_elem
= palette_data
[i
];
286 r
= (pal_elem
>> 16) & 0xff;
287 g
= (pal_elem
>> 8) & 0xff;
297 for (i
= 0; i
< PALETTE_COUNT
; i
++) {
298 pal_elem
= palette_data
[i
];
299 r
= (pal_elem
>> 16) & 0xff;
300 g
= (pal_elem
>> 8) & 0xff;
302 s
->palette
[i
* 4 + 0] = r
;
303 s
->palette
[i
* 4 + 1] = g
;
304 s
->palette
[i
* 4 + 2] = b
;
309 for (i
= 0; i
< PALETTE_COUNT
; i
++) {
310 pal_elem
= palette_data
[i
];
311 r
= (pal_elem
>> 16) & 0xff;
312 g
= (pal_elem
>> 8) & 0xff;
314 s
->palette
[i
* 4 + 0] = b
;
315 s
->palette
[i
* 4 + 1] = g
;
316 s
->palette
[i
* 4 + 2] = r
;
322 palette32
= (unsigned int *)s
->palette
;
323 memcpy (palette32
, palette_data
, PALETTE_COUNT
* sizeof(unsigned int));
326 case PIX_FMT_YUV444P
:
327 for (i
= 0; i
< PALETTE_COUNT
; i
++) {
328 pal_elem
= palette_data
[i
];
329 r
= (pal_elem
>> 16) & 0xff;
330 g
= (pal_elem
>> 8) & 0xff;
332 s
->palette
[i
* 4 + 0] = COMPUTE_Y(r
, g
, b
);
333 s
->palette
[i
* 4 + 1] = COMPUTE_U(r
, g
, b
);
334 s
->palette
[i
* 4 + 2] = COMPUTE_V(r
, g
, b
);
339 printf (" Xan WC3: Unhandled colorspace\n");
344 /* advance current_x variable; reset accounting variables if current_x
345 * moves beyond width */
346 #define ADVANCE_CURRENT_X() \
348 if (current_x >= width) { \
353 static void inline xan_wc3_output_pixel_run(XanContext
*s
,
354 unsigned char *pixel_buffer
, int x
, int y
, int pixel_count
)
360 int width
= s
->avctx
->width
;
362 unsigned char *palette_plane
;
363 unsigned char *y_plane
;
364 unsigned char *u_plane
;
365 unsigned char *v_plane
;
366 unsigned char *rgb_plane
;
367 unsigned short *rgb16_plane
;
368 unsigned short *palette16
;
369 unsigned int *rgb32_plane
;
370 unsigned int *palette32
;
372 switch (s
->avctx
->pix_fmt
) {
375 palette_plane
= s
->current_frame
.data
[0];
376 stride
= s
->current_frame
.linesize
[0];
377 line_inc
= stride
- width
;
378 index
= y
* stride
+ x
;
380 while(pixel_count
--) {
382 /* don't do a memcpy() here; keyframes generally copy an entire
383 * frame of data and the stride needs to be accounted for */
384 palette_plane
[index
++] = *pixel_buffer
++;
392 rgb16_plane
= (unsigned short *)s
->current_frame
.data
[0];
393 palette16
= (unsigned short *)s
->palette
;
394 stride
= s
->current_frame
.linesize
[0] / 2;
395 line_inc
= stride
- width
;
396 index
= y
* stride
+ x
;
398 while(pixel_count
--) {
400 rgb16_plane
[index
++] = palette16
[*pixel_buffer
++];
408 rgb_plane
= s
->current_frame
.data
[0];
409 stride
= s
->current_frame
.linesize
[0];
410 line_inc
= stride
- width
* 3;
411 index
= y
* stride
+ x
* 3;
413 while(pixel_count
--) {
414 pix
= *pixel_buffer
++;
416 rgb_plane
[index
++] = s
->palette
[pix
* 4 + 0];
417 rgb_plane
[index
++] = s
->palette
[pix
* 4 + 1];
418 rgb_plane
[index
++] = s
->palette
[pix
* 4 + 2];
425 rgb32_plane
= (unsigned int *)s
->current_frame
.data
[0];
426 palette32
= (unsigned int *)s
->palette
;
427 stride
= s
->current_frame
.linesize
[0] / 4;
428 line_inc
= stride
- width
;
429 index
= y
* stride
+ x
;
431 while(pixel_count
--) {
433 rgb32_plane
[index
++] = palette32
[*pixel_buffer
++];
439 case PIX_FMT_YUV444P
:
440 y_plane
= s
->current_frame
.data
[0];
441 u_plane
= s
->current_frame
.data
[1];
442 v_plane
= s
->current_frame
.data
[2];
443 stride
= s
->current_frame
.linesize
[0];
444 line_inc
= stride
- width
;
445 index
= y
* stride
+ x
;
447 while(pixel_count
--) {
448 pix
= *pixel_buffer
++;
450 y_plane
[index
] = s
->palette
[pix
* 4 + 0];
451 u_plane
[index
] = s
->palette
[pix
* 4 + 1];
452 v_plane
[index
] = s
->palette
[pix
* 4 + 2];
460 printf (" Xan WC3: Unhandled colorspace\n");
465 #define ADVANCE_CURFRAME_X() \
467 if (curframe_x >= width) { \
468 curframe_index += line_inc; \
472 #define ADVANCE_PREVFRAME_X() \
474 if (prevframe_x >= width) { \
475 prevframe_index += line_inc; \
479 static void inline xan_wc3_copy_pixel_run(XanContext
*s
,
480 int x
, int y
, int pixel_count
, int motion_x
, int motion_y
)
484 int curframe_index
, prevframe_index
;
485 int curframe_x
, prevframe_x
;
486 int width
= s
->avctx
->width
;
487 unsigned char *palette_plane
, *prev_palette_plane
;
488 unsigned char *y_plane
, *u_plane
, *v_plane
;
489 unsigned char *prev_y_plane
, *prev_u_plane
, *prev_v_plane
;
490 unsigned char *rgb_plane
, *prev_rgb_plane
;
491 unsigned short *rgb16_plane
, *prev_rgb16_plane
;
492 unsigned int *rgb32_plane
, *prev_rgb32_plane
;
494 switch (s
->avctx
->pix_fmt
) {
497 palette_plane
= s
->current_frame
.data
[0];
498 prev_palette_plane
= s
->last_frame
.data
[0];
499 stride
= s
->current_frame
.linesize
[0];
500 line_inc
= stride
- width
;
501 curframe_index
= y
* stride
+ x
;
503 prevframe_index
= (y
+ motion_y
) * stride
+ x
+ motion_x
;
504 prevframe_x
= x
+ motion_x
;
505 while(pixel_count
--) {
507 palette_plane
[curframe_index
++] =
508 prev_palette_plane
[prevframe_index
++];
510 ADVANCE_CURFRAME_X();
511 ADVANCE_PREVFRAME_X();
517 rgb16_plane
= (unsigned short *)s
->current_frame
.data
[0];
518 prev_rgb16_plane
= (unsigned short *)s
->last_frame
.data
[0];
519 stride
= s
->current_frame
.linesize
[0] / 2;
520 line_inc
= stride
- width
;
521 curframe_index
= y
* stride
+ x
;
523 prevframe_index
= (y
+ motion_y
) * stride
+ x
+ motion_x
;
524 prevframe_x
= x
+ motion_x
;
525 while(pixel_count
--) {
527 rgb16_plane
[curframe_index
++] =
528 prev_rgb16_plane
[prevframe_index
++];
530 ADVANCE_CURFRAME_X();
531 ADVANCE_PREVFRAME_X();
537 rgb_plane
= s
->current_frame
.data
[0];
538 prev_rgb_plane
= s
->last_frame
.data
[0];
539 stride
= s
->current_frame
.linesize
[0];
540 line_inc
= stride
- width
* 3;
541 curframe_index
= y
* stride
+ x
* 3;
543 prevframe_index
= (y
+ motion_y
) * stride
+
544 (3 * (x
+ motion_x
));
545 prevframe_x
= x
+ motion_x
;
546 while(pixel_count
--) {
548 rgb_plane
[curframe_index
++] = prev_rgb_plane
[prevframe_index
++];
549 rgb_plane
[curframe_index
++] = prev_rgb_plane
[prevframe_index
++];
550 rgb_plane
[curframe_index
++] = prev_rgb_plane
[prevframe_index
++];
552 ADVANCE_CURFRAME_X();
553 ADVANCE_PREVFRAME_X();
558 rgb32_plane
= (unsigned int *)s
->current_frame
.data
[0];
559 prev_rgb32_plane
= (unsigned int *)s
->last_frame
.data
[0];
560 stride
= s
->current_frame
.linesize
[0] / 4;
561 line_inc
= stride
- width
;
562 curframe_index
= y
* stride
+ x
;
564 prevframe_index
= (y
+ motion_y
) * stride
+ x
+ motion_x
;
565 prevframe_x
= x
+ motion_x
;
566 while(pixel_count
--) {
568 rgb32_plane
[curframe_index
++] =
569 prev_rgb32_plane
[prevframe_index
++];
571 ADVANCE_CURFRAME_X();
572 ADVANCE_PREVFRAME_X();
576 case PIX_FMT_YUV444P
:
577 y_plane
= s
->current_frame
.data
[0];
578 u_plane
= s
->current_frame
.data
[1];
579 v_plane
= s
->current_frame
.data
[2];
580 prev_y_plane
= s
->last_frame
.data
[0];
581 prev_u_plane
= s
->last_frame
.data
[1];
582 prev_v_plane
= s
->last_frame
.data
[2];
583 stride
= s
->current_frame
.linesize
[0];
584 line_inc
= stride
- width
;
585 curframe_index
= y
* stride
+ x
;
587 prevframe_index
= (y
+ motion_y
) * stride
+ x
+ motion_x
;
588 prevframe_x
= x
+ motion_x
;
589 while(pixel_count
--) {
591 y_plane
[curframe_index
] = prev_y_plane
[prevframe_index
];
592 u_plane
[curframe_index
] = prev_u_plane
[prevframe_index
];
593 v_plane
[curframe_index
] = prev_v_plane
[prevframe_index
];
596 ADVANCE_CURFRAME_X();
598 ADVANCE_PREVFRAME_X();
603 printf (" Xan WC3: Unhandled colorspace\n");
608 static void xan_wc3_decode_frame(XanContext
*s
) {
610 int width
= s
->avctx
->width
;
611 int height
= s
->avctx
->height
;
612 int total_pixels
= width
* height
;
613 unsigned char opcode
;
614 unsigned char flag
= 0;
616 int motion_x
, motion_y
;
619 unsigned char *opcode_buffer
= s
->buffer1
;
620 unsigned char *imagedata_buffer
= s
->buffer2
;
622 /* pointers to segments inside the compressed chunk */
623 unsigned char *huffman_segment
;
624 unsigned char *size_segment
;
625 unsigned char *vector_segment
;
626 unsigned char *imagedata_segment
;
628 huffman_segment
= s
->buf
+ LE_16(&s
->buf
[0]);
629 size_segment
= s
->buf
+ LE_16(&s
->buf
[2]);
630 vector_segment
= s
->buf
+ LE_16(&s
->buf
[4]);
631 imagedata_segment
= s
->buf
+ LE_16(&s
->buf
[6]);
633 xan_huffman_decode(opcode_buffer
, huffman_segment
);
635 if (imagedata_segment
[0] == 2)
636 xan_unpack(imagedata_buffer
, &imagedata_segment
[1]);
638 imagedata_buffer
= &imagedata_segment
[1];
640 /* use the decoded data segments to build the frame */
642 while (total_pixels
) {
644 opcode
= *opcode_buffer
++;
671 size
+= (opcode
- 10);
676 size
= *size_segment
++;
681 size
= BE_16(&size_segment
[0]);
687 size
= (size_segment
[0] << 16) | (size_segment
[1] << 8) |
696 /* run of (size) pixels is unchanged from last frame */
697 xan_wc3_copy_pixel_run(s
, x
, y
, size
, 0, 0);
699 /* output a run of pixels from imagedata_buffer */
700 xan_wc3_output_pixel_run(s
, imagedata_buffer
, x
, y
, size
);
701 imagedata_buffer
+= size
;
704 /* run-based motion compensation from last frame */
705 motion_x
= (*vector_segment
>> 4) & 0xF;
706 motion_y
= *vector_segment
& 0xF;
711 motion_x
|= 0xFFFFFFF0;
713 motion_y
|= 0xFFFFFFF0;
715 /* copy a run of pixels from the previous frame */
716 xan_wc3_copy_pixel_run(s
, x
, y
, size
, motion_x
, motion_y
);
721 /* coordinate accounting */
722 total_pixels
-= size
;
724 if (x
+ size
>= width
) {
735 /* for PAL8, make the palette available on the way out */
736 if (s
->avctx
->pix_fmt
== PIX_FMT_PAL8
) {
737 memcpy(s
->current_frame
.data
[1], s
->palette
, PALETTE_COUNT
* 4);
738 s
->current_frame
.palette_has_changed
= 1;
739 s
->avctx
->palctrl
->palette_changed
= 0;
743 static void xan_wc4_decode_frame(XanContext
*s
) {
746 static int xan_decode_frame(AVCodecContext
*avctx
,
747 void *data
, int *data_size
,
748 uint8_t *buf
, int buf_size
)
750 XanContext
*s
= avctx
->priv_data
;
751 AVPaletteControl
*palette_control
= avctx
->palctrl
;
754 if (palette_control
->palette_changed
) {
755 /* load the new palette and reset the palette control */
756 xan_wc3_build_palette(s
, palette_control
->palette
);
757 /* If pal8 we clear flag when we copy palette */
758 if (s
->avctx
->pix_fmt
!= PIX_FMT_PAL8
)
759 palette_control
->palette_changed
= 0;
763 if (avctx
->get_buffer(avctx
, &s
->current_frame
)) {
764 printf (" Xan Video: get_buffer() failed\n");
767 s
->current_frame
.reference
= 3;
772 if (avctx
->codec
->id
== CODEC_ID_XAN_WC3
)
773 xan_wc3_decode_frame(s
);
774 else if (avctx
->codec
->id
== CODEC_ID_XAN_WC4
)
775 xan_wc4_decode_frame(s
);
777 /* release the last frame if it is allocated */
778 if (s
->last_frame
.data
[0])
779 avctx
->release_buffer(avctx
, &s
->last_frame
);
782 s
->last_frame
= s
->current_frame
;
784 *data_size
= sizeof(AVFrame
);
785 *(AVFrame
*)data
= s
->current_frame
;
787 /* always report that the buffer was completely consumed */
791 static int xan_decode_end(AVCodecContext
*avctx
)
793 XanContext
*s
= avctx
->priv_data
;
795 /* release the last frame */
796 avctx
->release_buffer(avctx
, &s
->last_frame
);
804 AVCodec xan_wc3_decoder
= {
817 AVCodec xan_wc4_decoder = {