2 * Wing Commander/Xan Video Decoder
3 * Copyright (C) 2003 the ffmpeg project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * @file libavcodec/xan.c
24 * Xan video decoder for Wing Commander III computer game
25 * by Mario Brito (mbrito@student.dei.uc.pt)
26 * and Mike Melanson (melanson@pcisys.net)
28 * The xan_wc3 decoder outputs PAL8 data.
36 #include "libavutil/intreadwrite.h"
38 // for av_memcpy_backptr
39 #include "libavutil/lzo.h"
41 typedef struct XanContext {
43 AVCodecContext *avctx;
45 AVFrame current_frame;
47 const unsigned char *buf;
51 unsigned char *buffer1;
53 unsigned char *buffer2;
60 static av_cold int xan_decode_init(AVCodecContext *avctx)
62 XanContext *s = avctx->priv_data;
67 if ((avctx->codec->id == CODEC_ID_XAN_WC3) &&
68 (s->avctx->palctrl == NULL)) {
69 av_log(avctx, AV_LOG_ERROR, " WC3 Xan video: palette expected.\n");
73 avctx->pix_fmt = PIX_FMT_PAL8;
75 if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
78 s->buffer1_size = avctx->width * avctx->height;
79 s->buffer1 = av_malloc(s->buffer1_size);
80 s->buffer2_size = avctx->width * avctx->height;
81 s->buffer2 = av_malloc(s->buffer2_size + 12);
82 if (!s->buffer1 || !s->buffer2)
88 static int xan_huffman_decode(unsigned char *dest, const unsigned char *src,
91 unsigned char byte = *src++;
92 unsigned char ival = byte + 0x16;
93 const unsigned char * ptr = src + byte*2;
94 unsigned char val = ival;
96 unsigned char *dest_end = dest + dest_len;
98 unsigned char bits = *ptr++;
100 while ( val != 0x16 ) {
101 if ( (1 << counter) & bits )
102 val = src[byte + val - 0x17];
104 val = src[val - 0x17];
107 if (dest + 1 > dest_end)
113 if (counter++ == 7) {
123 * unpack simple compression
125 * @param dest destination buffer of dest_len, must be sufficiently padded for av_memcpy_backptr
127 static void xan_unpack(unsigned char *dest, const unsigned char *src, int dest_len)
129 unsigned char opcode;
132 int byte1, byte2, byte3;
133 unsigned char *dest_end = dest + dest_len;
138 if ( (opcode & 0x80) == 0 ) {
143 if (dest + size > dest_end)
145 memcpy(dest, src, size); dest += size; src += size;
147 size = ((opcode & 0x1c) >> 2) + 3;
148 if (dest + size > dest_end)
150 av_memcpy_backptr(dest, ((opcode & 0x60) << 3) + offset + 1, size);
153 } else if ( (opcode & 0x40) == 0 ) {
159 if (dest + size > dest_end)
161 memcpy(dest, src, size); dest += size; src += size;
163 size = (opcode & 0x3f) + 4;
164 if (dest + size > dest_end)
166 av_memcpy_backptr(dest, ((byte1 & 0x3f) << 8) + byte2 + 1, size);
169 } else if ( (opcode & 0x20) == 0 ) {
176 if (dest + size > dest_end)
178 memcpy(dest, src, size); dest += size; src += size;
180 size = byte3 + 5 + ((opcode & 0xc) << 6);
181 if (dest + size > dest_end)
183 av_memcpy_backptr(dest,
184 ((opcode & 0x10) << 12) + 1 + (byte1 << 8) + byte2,
188 size = ((opcode & 0x1f) << 2) + 4;
193 if (dest + size > dest_end)
195 memcpy(dest, src, size); dest += size; src += size;
200 memcpy(dest, src, size); dest += size; src += size;
203 static inline void xan_wc3_output_pixel_run(XanContext *s,
204 const unsigned char *pixel_buffer, int x, int y, int pixel_count)
210 int width = s->avctx->width;
211 unsigned char *palette_plane;
213 palette_plane = s->current_frame.data[0];
214 stride = s->current_frame.linesize[0];
215 line_inc = stride - width;
216 index = y * stride + x;
218 while((pixel_count--) && (index < s->frame_size)) {
220 /* don't do a memcpy() here; keyframes generally copy an entire
221 * frame of data and the stride needs to be accounted for */
222 palette_plane[index++] = *pixel_buffer++;
225 if (current_x >= width) {
232 static inline void xan_wc3_copy_pixel_run(XanContext *s,
233 int x, int y, int pixel_count, int motion_x, int motion_y)
237 int curframe_index, prevframe_index;
238 int curframe_x, prevframe_x;
239 int width = s->avctx->width;
240 unsigned char *palette_plane, *prev_palette_plane;
242 palette_plane = s->current_frame.data[0];
243 prev_palette_plane = s->last_frame.data[0];
244 stride = s->current_frame.linesize[0];
245 line_inc = stride - width;
246 curframe_index = y * stride + x;
248 prevframe_index = (y + motion_y) * stride + x + motion_x;
249 prevframe_x = x + motion_x;
250 while((pixel_count--) && (curframe_index < s->frame_size)) {
252 palette_plane[curframe_index++] =
253 prev_palette_plane[prevframe_index++];
256 if (curframe_x >= width) {
257 curframe_index += line_inc;
262 if (prevframe_x >= width) {
263 prevframe_index += line_inc;
269 static void xan_wc3_decode_frame(XanContext *s) {
271 int width = s->avctx->width;
272 int height = s->avctx->height;
273 int total_pixels = width * height;
274 unsigned char opcode;
275 unsigned char flag = 0;
277 int motion_x, motion_y;
280 unsigned char *opcode_buffer = s->buffer1;
281 int opcode_buffer_size = s->buffer1_size;
282 const unsigned char *imagedata_buffer = s->buffer2;
284 /* pointers to segments inside the compressed chunk */
285 const unsigned char *huffman_segment;
286 const unsigned char *size_segment;
287 const unsigned char *vector_segment;
288 const unsigned char *imagedata_segment;
290 huffman_segment = s->buf + AV_RL16(&s->buf[0]);
291 size_segment = s->buf + AV_RL16(&s->buf[2]);
292 vector_segment = s->buf + AV_RL16(&s->buf[4]);
293 imagedata_segment = s->buf + AV_RL16(&s->buf[6]);
295 xan_huffman_decode(opcode_buffer, huffman_segment, opcode_buffer_size);
297 if (imagedata_segment[0] == 2)
298 xan_unpack(s->buffer2, &imagedata_segment[1], s->buffer2_size);
300 imagedata_buffer = &imagedata_segment[1];
302 /* use the decoded data segments to build the frame */
304 while (total_pixels) {
306 opcode = *opcode_buffer++;
333 size += (opcode - 10);
338 size = *size_segment++;
343 size = AV_RB16(&size_segment[0]);
349 size = AV_RB24(size_segment);
357 /* run of (size) pixels is unchanged from last frame */
358 xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
360 /* output a run of pixels from imagedata_buffer */
361 xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);
362 imagedata_buffer += size;
365 /* run-based motion compensation from last frame */
366 motion_x = (*vector_segment >> 4) & 0xF;
367 motion_y = *vector_segment & 0xF;
372 motion_x |= 0xFFFFFFF0;
374 motion_y |= 0xFFFFFFF0;
376 /* copy a run of pixels from the previous frame */
377 xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
382 /* coordinate accounting */
383 total_pixels -= size;
385 if (x + size >= width) {
397 static void xan_wc4_decode_frame(XanContext *s) {
400 static int xan_decode_frame(AVCodecContext *avctx,
401 void *data, int *data_size,
404 const uint8_t *buf = avpkt->data;
405 int buf_size = avpkt->size;
406 XanContext *s = avctx->priv_data;
407 AVPaletteControl *palette_control = avctx->palctrl;
409 if (avctx->get_buffer(avctx, &s->current_frame)) {
410 av_log(s->avctx, AV_LOG_ERROR, " Xan Video: get_buffer() failed\n");
413 s->current_frame.reference = 3;
416 s->frame_size = s->current_frame.linesize[0] * s->avctx->height;
418 palette_control->palette_changed = 0;
419 memcpy(s->current_frame.data[1], palette_control->palette,
421 s->current_frame.palette_has_changed = 1;
426 if (avctx->codec->id == CODEC_ID_XAN_WC3)
427 xan_wc3_decode_frame(s);
428 else if (avctx->codec->id == CODEC_ID_XAN_WC4)
429 xan_wc4_decode_frame(s);
431 /* release the last frame if it is allocated */
432 if (s->last_frame.data[0])
433 avctx->release_buffer(avctx, &s->last_frame);
435 *data_size = sizeof(AVFrame);
436 *(AVFrame*)data = s->current_frame;
439 FFSWAP(AVFrame, s->current_frame, s->last_frame);
441 /* always report that the buffer was completely consumed */
445 static av_cold int xan_decode_end(AVCodecContext *avctx)
447 XanContext *s = avctx->priv_data;
449 /* release the frames */
450 if (s->last_frame.data[0])
451 avctx->release_buffer(avctx, &s->last_frame);
452 if (s->current_frame.data[0])
453 avctx->release_buffer(avctx, &s->current_frame);
461 AVCodec xan_wc3_decoder = {
471 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III / Xan"),
475 AVCodec xan_wc4_decoder = {