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.
35 #include "libavutil/intreadwrite.h"
37 #include "bytestream.h"
38 #define ALT_BITSTREAM_READER_LE
40 // for av_memcpy_backptr
41 #include "libavutil/lzo.h"
43 typedef struct XanContext {
45 AVCodecContext *avctx;
47 AVFrame current_frame;
49 const unsigned char *buf;
53 unsigned char *buffer1;
55 unsigned char *buffer2;
62 static av_cold int xan_decode_init(AVCodecContext *avctx)
64 XanContext *s = avctx->priv_data;
69 if ((avctx->codec->id == CODEC_ID_XAN_WC3) &&
70 (s->avctx->palctrl == NULL)) {
71 av_log(avctx, AV_LOG_ERROR, " WC3 Xan video: palette expected.\n");
75 avctx->pix_fmt = PIX_FMT_PAL8;
77 if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
80 s->buffer1_size = avctx->width * avctx->height;
81 s->buffer1 = av_malloc(s->buffer1_size);
82 s->buffer2_size = avctx->width * avctx->height;
83 s->buffer2 = av_malloc(s->buffer2_size + 130);
84 if (!s->buffer1 || !s->buffer2)
90 static int xan_huffman_decode(unsigned char *dest, const unsigned char *src,
93 unsigned char byte = *src++;
94 unsigned char ival = byte + 0x16;
95 const unsigned char * ptr = src + byte*2;
96 unsigned char val = ival;
97 unsigned char *dest_end = dest + dest_len;
100 init_get_bits(&gb, ptr, 0); // FIXME: no src size available
102 while ( val != 0x16 ) {
103 val = src[val - 0x17 + get_bits1(&gb) * byte];
106 if (dest >= dest_end)
117 * unpack simple compression
119 * @param dest destination buffer of dest_len, must be padded with at least 130 bytes
121 static void xan_unpack(unsigned char *dest, const unsigned char *src, int dest_len)
123 unsigned char opcode;
125 unsigned char *dest_end = dest + dest_len;
127 while (dest < dest_end) {
132 if ( (opcode & 0x80) == 0 ) {
136 back = ((opcode & 0x60) << 3) + *src++ + 1;
137 size2 = ((opcode & 0x1c) >> 2) + 3;
139 } else if ( (opcode & 0x40) == 0 ) {
143 back = (bytestream_get_be16(&src) & 0x3fff) + 1;
144 size2 = (opcode & 0x3f) + 4;
150 back = ((opcode & 0x10) << 12) + bytestream_get_be16(&src) + 1;
151 size2 = ((opcode & 0x0c) << 6) + *src++ + 5;
152 if (size + size2 > dest_end - dest)
155 memcpy(dest, src, size); dest += size; src += size;
156 av_memcpy_backptr(dest, back, size2);
159 int finish = opcode >= 0xfc;
160 size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
162 memcpy(dest, src, size); dest += size; src += size;
169 static inline void xan_wc3_output_pixel_run(XanContext *s,
170 const unsigned char *pixel_buffer, int x, int y, int pixel_count)
176 int width = s->avctx->width;
177 unsigned char *palette_plane;
179 palette_plane = s->current_frame.data[0];
180 stride = s->current_frame.linesize[0];
181 line_inc = stride - width;
182 index = y * stride + x;
184 while((pixel_count--) && (index < s->frame_size)) {
186 /* don't do a memcpy() here; keyframes generally copy an entire
187 * frame of data and the stride needs to be accounted for */
188 palette_plane[index++] = *pixel_buffer++;
191 if (current_x >= width) {
198 static inline void xan_wc3_copy_pixel_run(XanContext *s,
199 int x, int y, int pixel_count, int motion_x, int motion_y)
203 int curframe_index, prevframe_index;
204 int curframe_x, prevframe_x;
205 int width = s->avctx->width;
206 unsigned char *palette_plane, *prev_palette_plane;
208 palette_plane = s->current_frame.data[0];
209 prev_palette_plane = s->last_frame.data[0];
210 stride = s->current_frame.linesize[0];
211 line_inc = stride - width;
212 curframe_index = y * stride + x;
214 prevframe_index = (y + motion_y) * stride + x + motion_x;
215 prevframe_x = x + motion_x;
216 while((pixel_count--) && (curframe_index < s->frame_size)) {
218 palette_plane[curframe_index++] =
219 prev_palette_plane[prevframe_index++];
222 if (curframe_x >= width) {
223 curframe_index += line_inc;
228 if (prevframe_x >= width) {
229 prevframe_index += line_inc;
235 static void xan_wc3_decode_frame(XanContext *s) {
237 int width = s->avctx->width;
238 int height = s->avctx->height;
239 int total_pixels = width * height;
240 unsigned char opcode;
241 unsigned char flag = 0;
243 int motion_x, motion_y;
246 unsigned char *opcode_buffer = s->buffer1;
247 int opcode_buffer_size = s->buffer1_size;
248 const unsigned char *imagedata_buffer = s->buffer2;
250 /* pointers to segments inside the compressed chunk */
251 const unsigned char *huffman_segment;
252 const unsigned char *size_segment;
253 const unsigned char *vector_segment;
254 const unsigned char *imagedata_segment;
256 huffman_segment = s->buf + AV_RL16(&s->buf[0]);
257 size_segment = s->buf + AV_RL16(&s->buf[2]);
258 vector_segment = s->buf + AV_RL16(&s->buf[4]);
259 imagedata_segment = s->buf + AV_RL16(&s->buf[6]);
261 xan_huffman_decode(opcode_buffer, huffman_segment, opcode_buffer_size);
263 if (imagedata_segment[0] == 2)
264 xan_unpack(s->buffer2, &imagedata_segment[1], s->buffer2_size);
266 imagedata_buffer = &imagedata_segment[1];
268 /* use the decoded data segments to build the frame */
270 while (total_pixels) {
272 opcode = *opcode_buffer++;
299 size += (opcode - 10);
304 size = *size_segment++;
309 size = AV_RB16(&size_segment[0]);
315 size = AV_RB24(size_segment);
323 /* run of (size) pixels is unchanged from last frame */
324 xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
326 /* output a run of pixels from imagedata_buffer */
327 xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);
328 imagedata_buffer += size;
331 /* run-based motion compensation from last frame */
332 motion_x = sign_extend(*vector_segment >> 4, 4);
333 motion_y = sign_extend(*vector_segment & 0xF, 4);
336 /* copy a run of pixels from the previous frame */
337 xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
342 /* coordinate accounting */
343 total_pixels -= size;
344 y += (x + size) / width;
345 x = (x + size) % width;
349 static void xan_wc4_decode_frame(XanContext *s) {
352 static int xan_decode_frame(AVCodecContext *avctx,
353 void *data, int *data_size,
356 const uint8_t *buf = avpkt->data;
357 int buf_size = avpkt->size;
358 XanContext *s = avctx->priv_data;
359 AVPaletteControl *palette_control = avctx->palctrl;
361 if (avctx->get_buffer(avctx, &s->current_frame)) {
362 av_log(s->avctx, AV_LOG_ERROR, " Xan Video: get_buffer() failed\n");
365 s->current_frame.reference = 3;
368 s->frame_size = s->current_frame.linesize[0] * s->avctx->height;
370 palette_control->palette_changed = 0;
371 memcpy(s->current_frame.data[1], palette_control->palette,
373 s->current_frame.palette_has_changed = 1;
378 if (avctx->codec->id == CODEC_ID_XAN_WC3)
379 xan_wc3_decode_frame(s);
380 else if (avctx->codec->id == CODEC_ID_XAN_WC4)
381 xan_wc4_decode_frame(s);
383 /* release the last frame if it is allocated */
384 if (s->last_frame.data[0])
385 avctx->release_buffer(avctx, &s->last_frame);
387 *data_size = sizeof(AVFrame);
388 *(AVFrame*)data = s->current_frame;
391 FFSWAP(AVFrame, s->current_frame, s->last_frame);
393 /* always report that the buffer was completely consumed */
397 static av_cold int xan_decode_end(AVCodecContext *avctx)
399 XanContext *s = avctx->priv_data;
401 /* release the frames */
402 if (s->last_frame.data[0])
403 avctx->release_buffer(avctx, &s->last_frame);
404 if (s->current_frame.data[0])
405 avctx->release_buffer(avctx, &s->current_frame);
413 AVCodec xan_wc3_decoder = {
423 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III / Xan"),
427 AVCodec xan_wc4_decoder = {