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)
26 * For more information about the Xan format, visit:
27 * http://www.pcisys.net/~melanson/codecs/
39 #define PALETTE_COUNT 256
40 #define PALETTE_CONTROL_SIZE ((256 * 3) + 1)
42 typedef struct XanContext {
44 AVCodecContext *avctx;
47 AVFrame current_frame;
52 unsigned char palette[PALETTE_COUNT * 4];
55 unsigned char *buffer1;
56 unsigned char *buffer2;
60 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
61 #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
62 #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
63 (((uint8_t*)(x))[2] << 16) | \
64 (((uint8_t*)(x))[1] << 8) | \
67 /* RGB -> YUV conversion stuff */
68 #define SCALEFACTOR 65536
69 #define CENTERSAMPLE 128
71 #define COMPUTE_Y(r, g, b) \
73 ((y_r_table[r] + y_g_table[g] + y_b_table[b]) / SCALEFACTOR)
74 #define COMPUTE_U(r, g, b) \
76 ((u_r_table[r] + u_g_table[g] + u_b_table[b]) / SCALEFACTOR + CENTERSAMPLE)
77 #define COMPUTE_V(r, g, b) \
79 ((v_r_table[r] + v_g_table[g] + v_b_table[b]) / SCALEFACTOR + CENTERSAMPLE)
81 #define Y_R (SCALEFACTOR * 0.29900)
82 #define Y_G (SCALEFACTOR * 0.58700)
83 #define Y_B (SCALEFACTOR * 0.11400)
85 #define U_R (SCALEFACTOR * -0.16874)
86 #define U_G (SCALEFACTOR * -0.33126)
87 #define U_B (SCALEFACTOR * 0.50000)
89 #define V_R (SCALEFACTOR * 0.50000)
90 #define V_G (SCALEFACTOR * -0.41869)
91 #define V_B (SCALEFACTOR * -0.08131)
94 * Precalculate all of the YUV tables since it requires fewer than
95 * 10 kilobytes to store them.
97 static int y_r_table[256];
98 static int y_g_table[256];
99 static int y_b_table[256];
101 static int u_r_table[256];
102 static int u_g_table[256];
103 static int u_b_table[256];
105 static int v_r_table[256];
106 static int v_g_table[256];
107 static int v_b_table[256];
109 static int xan_decode_init(AVCodecContext *avctx)
111 XanContext *s = avctx->priv_data;
116 if ((avctx->codec->id == CODEC_ID_XAN_WC3) &&
117 (s->avctx->extradata_size != PALETTE_CONTROL_SIZE)) {
118 printf (" WC3 Xan video: expected extradata_size of %d\n",
119 PALETTE_CONTROL_SIZE);
123 avctx->pix_fmt = PIX_FMT_YUV444P;
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 * 4);
143 s->buffer2 = av_malloc(avctx->width * avctx->height * 4);
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_decode_method_1(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 int xan_decode_method_2(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;
256 static void inline xan_wc3_build_palette(XanContext *s,
257 unsigned char *palette_data)
260 unsigned char r, g, b;
262 /* transform the palette passed through the palette control structure
263 * into the necessary internal format depending on colorspace */
265 switch (s->avctx->pix_fmt) {
267 case PIX_FMT_YUV444P:
268 for (i = 0; i < PALETTE_COUNT; i++) {
272 s->palette[i * 4 + 0] = COMPUTE_Y(r, g, b);
273 s->palette[i * 4 + 1] = COMPUTE_U(r, g, b);
274 s->palette[i * 4 + 2] = COMPUTE_V(r, g, b);
279 printf (" Xan WC3: Unhandled colorspace\n");
284 static void inline xan_wc3_output_pixel_run(XanContext *s,
285 unsigned char *pixel_buffer, int x, int y, int pixel_count)
291 int width = s->avctx->width;
293 unsigned char *y_plane;
294 unsigned char *u_plane;
295 unsigned char *v_plane;
297 switch (s->avctx->pix_fmt) {
299 case PIX_FMT_YUV444P:
300 y_plane = s->current_frame.data[0];
301 u_plane = s->current_frame.data[1];
302 v_plane = s->current_frame.data[2];
303 stride = s->current_frame.linesize[0];
304 line_inc = stride - width;
305 index = y * stride + x;
307 while(pixel_count--) {
308 pixel = *pixel_buffer++;
310 y_plane[index] = s->palette[pixel * 4 + 0];
311 u_plane[index] = s->palette[pixel * 4 + 1];
312 v_plane[index] = s->palette[pixel * 4 + 2];
316 if (current_x >= width) {
317 /* reset accounting variables */
325 printf (" Xan WC3: Unhandled colorspace\n");
330 static void inline xan_wc3_copy_pixel_run(XanContext *s,
331 int x, int y, int pixel_count, int motion_x, int motion_y)
335 int curframe_index, prevframe_index;
336 int curframe_x, prevframe_x;
337 int width = s->avctx->width;
338 unsigned char *y_plane, *u_plane, *v_plane;
339 unsigned char *prev_y_plane, *prev_u_plane, *prev_v_plane;
341 switch (s->avctx->pix_fmt) {
343 case PIX_FMT_YUV444P:
344 y_plane = s->current_frame.data[0];
345 u_plane = s->current_frame.data[1];
346 v_plane = s->current_frame.data[2];
347 prev_y_plane = s->last_frame.data[0];
348 prev_u_plane = s->last_frame.data[1];
349 prev_v_plane = s->last_frame.data[2];
350 stride = s->current_frame.linesize[0];
351 line_inc = stride - width;
352 curframe_index = y * stride + x;
354 prevframe_index = (y + motion_x) * stride + x + motion_x;
355 prevframe_x = x + motion_x;
356 while(pixel_count--) {
358 y_plane[curframe_index] = prev_y_plane[prevframe_index];
359 u_plane[curframe_index] = prev_u_plane[prevframe_index];
360 v_plane[curframe_index] = prev_v_plane[prevframe_index];
364 if (curframe_x >= width) {
365 /* reset accounting variables */
366 curframe_index += line_inc;
372 if (prevframe_x >= width) {
373 /* reset accounting variables */
374 prevframe_index += line_inc;
381 printf (" Xan WC3: Unhandled colorspace\n");
386 static void xan_wc3_decode_frame(XanContext *s) {
388 int width = s->avctx->width;
389 int height = s->avctx->height;
390 int total_pixels = width * height;
391 unsigned char opcode;
392 unsigned char flag = 0;
394 int motion_x, motion_y;
397 unsigned char *method1_buffer = s->buffer1;
398 unsigned char *method2_buffer = s->buffer2;
400 /* pointers to segments inside the compressed chunk */
401 unsigned char *method1_segment;
402 unsigned char *size_segment;
403 unsigned char *vector_segment;
404 unsigned char *method2_segment;
406 method1_segment = s->buf + LE_16(&s->buf[0]);
407 size_segment = s->buf + LE_16(&s->buf[2]);
408 vector_segment = s->buf + LE_16(&s->buf[4]);
409 method2_segment = s->buf + LE_16(&s->buf[6]);
411 xan_decode_method_1(method1_buffer, method1_segment);
412 if (method2_segment[0] == 2)
413 xan_decode_method_2(method2_buffer, method2_segment + 1);
415 method2_buffer = method2_segment + 1;
417 /* use the decoded data segments to build the frame */
419 while (total_pixels) {
421 opcode = *method1_buffer++;
448 size += (opcode - 10);
453 size = *size_segment++;
458 size = BE_16(&size_segment[0]);
464 size = (size_segment[0] << 16) | (size_segment[1] << 8) |
473 /* run of (size) pixels is unchanged from last frame */
474 xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
476 /* output a run of pixels from method2_buffer */
477 xan_wc3_output_pixel_run(s, method2_buffer, x, y, size);
478 method2_buffer += size;
481 /* run-based motion compensation from last frame */
482 motion_x = (*vector_segment >> 4) & 0xF;
483 motion_y = *vector_segment & 0xF;
488 motion_x |= 0xFFFFFFF0;
490 motion_y |= 0xFFFFFFF0;
492 /* copy a run of pixels from the previous frame */
493 xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
498 /* coordinate accounting */
499 total_pixels -= size;
501 if (x + size >= width) {
513 static void xan_wc4_decode_frame(XanContext *s) {
516 static int xan_decode_frame(AVCodecContext *avctx,
517 void *data, int *data_size,
518 uint8_t *buf, int buf_size)
520 XanContext *s = avctx->priv_data;
521 unsigned char *palette_control = avctx->extradata;
524 if (palette_control[0]) {
525 /* load the new palette and reset the palette control */
526 xan_wc3_build_palette(s, &palette_control[1]);
527 palette_control[0] = 0;
531 if (avctx->get_buffer(avctx, &s->current_frame)) {
532 printf (" Interplay Video: get_buffer() failed\n");
539 if (avctx->codec->id == CODEC_ID_XAN_WC3) {
542 xan_wc3_decode_frame(s);
544 memcpy(s->current_frame.data[0], s->last_frame.data[0],
545 s->current_frame.linesize[0] * avctx->height);
546 memcpy(s->current_frame.data[1], s->last_frame.data[1],
547 s->current_frame.linesize[1] * avctx->height);
548 memcpy(s->current_frame.data[2], s->last_frame.data[2],
549 s->current_frame.linesize[2] * avctx->height);
551 } else if (avctx->codec->id == CODEC_ID_XAN_WC4)
552 xan_wc4_decode_frame(s);
554 /* release the last frame if it is allocated */
555 if (s->last_frame.data[0])
556 avctx->release_buffer(avctx, &s->last_frame);
559 s->last_frame = s->current_frame;
561 *data_size = sizeof(AVFrame);
562 *(AVFrame*)data = s->current_frame;
564 /* always report that the buffer was completely consumed */
568 static int xan_decode_end(AVCodecContext *avctx)
570 XanContext *s = avctx->priv_data;
572 /* release the last frame */
573 avctx->release_buffer(avctx, &s->last_frame);
581 AVCodec xan_wc3_decoder = {
594 AVCodec xan_wc4_decoder = {