]> git.sesse.net Git - ffmpeg/blob - libavcodec/xan.c
c0e2db784f494b39f8af78c713fc9d46f667f0fe
[ffmpeg] / libavcodec / xan.c
1 /*
2  * Wing Commander/Xan Video Decoder
3  * Copyright (C) 2003 the ffmpeg project
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
23 /**
24  * @file xan.c
25  * Xan video decoder for Wing Commander III computer game
26  * by Mario Brito (mbrito@student.dei.uc.pt)
27  * and Mike Melanson (melanson@pcisys.net)
28  *
29  * The xan_wc3 decoder outputs PAL8 data.
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36
37 #include "common.h"
38 #include "avcodec.h"
39
40 typedef struct XanContext {
41
42     AVCodecContext *avctx;
43     AVFrame last_frame;
44     AVFrame current_frame;
45
46     unsigned char *buf;
47     int size;
48
49     /* scratch space */
50     unsigned char *buffer1;
51     int buffer1_size;
52     unsigned char *buffer2;
53     int buffer2_size;
54
55     int frame_size;
56
57 } XanContext;
58
59 static int xan_decode_init(AVCodecContext *avctx)
60 {
61     XanContext *s = avctx->priv_data;
62
63     s->avctx = avctx;
64     s->frame_size = 0;
65
66     if ((avctx->codec->id == CODEC_ID_XAN_WC3) &&
67         (s->avctx->palctrl == NULL)) {
68         av_log(avctx, AV_LOG_ERROR, " WC3 Xan video: palette expected.\n");
69         return -1;
70     }
71
72     avctx->pix_fmt = PIX_FMT_PAL8;
73
74     if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
75         return -1;
76
77     s->buffer1_size = avctx->width * avctx->height;
78     s->buffer1 = av_malloc(s->buffer1_size);
79     s->buffer2_size = avctx->width * avctx->height;
80     s->buffer2 = av_malloc(s->buffer2_size);
81     if (!s->buffer1 || !s->buffer2)
82         return -1;
83
84     return 0;
85 }
86
87 /* This function is used in lieu of memcpy(). This decoder cannot use
88  * memcpy because the memory locations often overlap and
89  * memcpy doesn't like that; it's not uncommon, for example, for
90  * dest = src+1, to turn byte A into  pattern AAAAAAAA.
91  * This was originally repz movsb in Intel x86 ASM. */
92 static inline void bytecopy(unsigned char *dest, unsigned char *src, int count)
93 {
94     int i;
95
96     for (i = 0; i < count; i++)
97         dest[i] = src[i];
98 }
99
100 static int xan_huffman_decode(unsigned char *dest, unsigned char *src,
101     int dest_len)
102 {
103     unsigned char byte = *src++;
104     unsigned char ival = byte + 0x16;
105     unsigned char * ptr = src + byte*2;
106     unsigned char val = ival;
107     int counter = 0;
108     unsigned char *dest_end = dest + dest_len;
109
110     unsigned char bits = *ptr++;
111
112     while ( val != 0x16 ) {
113         if ( (1 << counter) & bits )
114             val = src[byte + val - 0x17];
115         else
116             val = src[val - 0x17];
117
118         if ( val < 0x16 ) {
119             if (dest + 1 > dest_end)
120                 return 0;
121             *dest++ = val;
122             val = ival;
123         }
124
125         if (counter++ == 7) {
126             counter = 0;
127             bits = *ptr++;
128         }
129     }
130
131     return 0;
132 }
133
134 static void xan_unpack(unsigned char *dest, unsigned char *src, int dest_len)
135 {
136     unsigned char opcode;
137     int size;
138     int offset;
139     int byte1, byte2, byte3;
140     unsigned char *dest_end = dest + dest_len;
141
142     for (;;) {
143         opcode = *src++;
144
145         if ( (opcode & 0x80) == 0 ) {
146
147             offset = *src++;
148
149             size = opcode & 3;
150             if (dest + size > dest_end)
151                 return;
152             bytecopy(dest, src, size);  dest += size;  src += size;
153
154             size = ((opcode & 0x1c) >> 2) + 3;
155             if (dest + size > dest_end)
156                 return;
157             bytecopy (dest, dest - (((opcode & 0x60) << 3) + offset + 1), size);
158             dest += size;
159
160         } else if ( (opcode & 0x40) == 0 ) {
161
162             byte1 = *src++;
163             byte2 = *src++;
164
165             size = byte1 >> 6;
166             if (dest + size > dest_end)
167                 return;
168             bytecopy (dest, src, size);  dest += size;  src += size;
169
170             size = (opcode & 0x3f) + 4;
171             if (dest + size > dest_end)
172                 return;
173             bytecopy (dest, dest - (((byte1 & 0x3f) << 8) + byte2 + 1), size);
174             dest += size;
175
176         } else if ( (opcode & 0x20) == 0 ) {
177
178             byte1 = *src++;
179             byte2 = *src++;
180             byte3 = *src++;
181
182             size = opcode & 3;
183             if (dest + size > dest_end)
184                 return;
185             bytecopy (dest, src, size);  dest += size;  src += size;
186
187             size = byte3 + 5 + ((opcode & 0xc) << 6);
188             if (dest + size > dest_end)
189                 return;
190             bytecopy (dest,
191                 dest - ((((opcode & 0x10) >> 4) << 0x10) + 1 + (byte1 << 8) + byte2),
192                 size);
193             dest += size;
194         } else {
195             size = ((opcode & 0x1f) << 2) + 4;
196
197             if (size > 0x70)
198                 break;
199
200             if (dest + size > dest_end)
201                 return;
202             bytecopy (dest, src, size);  dest += size;  src += size;
203         }
204     }
205
206     size = opcode & 3;
207     bytecopy(dest, src, size);  dest += size;  src += size;
208 }
209
210 static inline void xan_wc3_output_pixel_run(XanContext *s,
211     unsigned char *pixel_buffer, int x, int y, int pixel_count)
212 {
213     int stride;
214     int line_inc;
215     int index;
216     int current_x;
217     int width = s->avctx->width;
218     unsigned char *palette_plane;
219
220     palette_plane = s->current_frame.data[0];
221     stride = s->current_frame.linesize[0];
222     line_inc = stride - width;
223     index = y * stride + x;
224     current_x = x;
225     while((pixel_count--) && (index < s->frame_size)) {
226
227         /* don't do a memcpy() here; keyframes generally copy an entire
228          * frame of data and the stride needs to be accounted for */
229         palette_plane[index++] = *pixel_buffer++;
230
231         current_x++;
232         if (current_x >= width) {
233             index += line_inc;
234             current_x = 0;
235         }
236     }
237 }
238
239 static inline void xan_wc3_copy_pixel_run(XanContext *s,
240     int x, int y, int pixel_count, int motion_x, int motion_y)
241 {
242     int stride;
243     int line_inc;
244     int curframe_index, prevframe_index;
245     int curframe_x, prevframe_x;
246     int width = s->avctx->width;
247     unsigned char *palette_plane, *prev_palette_plane;
248
249     palette_plane = s->current_frame.data[0];
250     prev_palette_plane = s->last_frame.data[0];
251     stride = s->current_frame.linesize[0];
252     line_inc = stride - width;
253     curframe_index = y * stride + x;
254     curframe_x = x;
255     prevframe_index = (y + motion_y) * stride + x + motion_x;
256     prevframe_x = x + motion_x;
257     while((pixel_count--) && (curframe_index < s->frame_size)) {
258
259         palette_plane[curframe_index++] =
260             prev_palette_plane[prevframe_index++];
261
262         curframe_x++;
263         if (curframe_x >= width) {
264             curframe_index += line_inc;
265             curframe_x = 0;
266         }
267
268         prevframe_x++;
269         if (prevframe_x >= width) {
270             prevframe_index += line_inc;
271             prevframe_x = 0;
272         }
273     }
274 }
275
276 static void xan_wc3_decode_frame(XanContext *s) {
277
278     int width = s->avctx->width;
279     int height = s->avctx->height;
280     int total_pixels = width * height;
281     unsigned char opcode;
282     unsigned char flag = 0;
283     int size = 0;
284     int motion_x, motion_y;
285     int x, y;
286
287     unsigned char *opcode_buffer = s->buffer1;
288     int opcode_buffer_size = s->buffer1_size;
289     unsigned char *imagedata_buffer = s->buffer2;
290     int imagedata_buffer_size = s->buffer2_size;
291
292     /* pointers to segments inside the compressed chunk */
293     unsigned char *huffman_segment;
294     unsigned char *size_segment;
295     unsigned char *vector_segment;
296     unsigned char *imagedata_segment;
297
298     huffman_segment =   s->buf + AV_RL16(&s->buf[0]);
299     size_segment =      s->buf + AV_RL16(&s->buf[2]);
300     vector_segment =    s->buf + AV_RL16(&s->buf[4]);
301     imagedata_segment = s->buf + AV_RL16(&s->buf[6]);
302
303     xan_huffman_decode(opcode_buffer, huffman_segment, opcode_buffer_size);
304
305     if (imagedata_segment[0] == 2)
306         xan_unpack(imagedata_buffer, &imagedata_segment[1],
307             imagedata_buffer_size);
308     else
309         imagedata_buffer = &imagedata_segment[1];
310
311     /* use the decoded data segments to build the frame */
312     x = y = 0;
313     while (total_pixels) {
314
315         opcode = *opcode_buffer++;
316         size = 0;
317
318         switch (opcode) {
319
320         case 0:
321             flag ^= 1;
322             continue;
323
324         case 1:
325         case 2:
326         case 3:
327         case 4:
328         case 5:
329         case 6:
330         case 7:
331         case 8:
332             size = opcode;
333             break;
334
335         case 12:
336         case 13:
337         case 14:
338         case 15:
339         case 16:
340         case 17:
341         case 18:
342             size += (opcode - 10);
343             break;
344
345         case 9:
346         case 19:
347             size = *size_segment++;
348             break;
349
350         case 10:
351         case 20:
352             size = AV_RB16(&size_segment[0]);
353             size_segment += 2;
354             break;
355
356         case 11:
357         case 21:
358             size = (size_segment[0] << 16) | (size_segment[1] << 8) |
359                 size_segment[2];
360             size_segment += 3;
361             break;
362         }
363
364         if (opcode < 12) {
365             flag ^= 1;
366             if (flag) {
367                 /* run of (size) pixels is unchanged from last frame */
368                 xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
369             } else {
370                 /* output a run of pixels from imagedata_buffer */
371                 xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);
372                 imagedata_buffer += size;
373             }
374         } else {
375             /* run-based motion compensation from last frame */
376             motion_x = (*vector_segment >> 4) & 0xF;
377             motion_y = *vector_segment & 0xF;
378             vector_segment++;
379
380             /* sign extension */
381             if (motion_x & 0x8)
382                 motion_x |= 0xFFFFFFF0;
383             if (motion_y & 0x8)
384                 motion_y |= 0xFFFFFFF0;
385
386             /* copy a run of pixels from the previous frame */
387             xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
388
389             flag = 0;
390         }
391
392         /* coordinate accounting */
393         total_pixels -= size;
394         while (size) {
395             if (x + size >= width) {
396                 y++;
397                 size -= (width - x);
398                 x = 0;
399             } else {
400                 x += size;
401                 size = 0;
402             }
403         }
404     }
405 }
406
407 static void xan_wc4_decode_frame(XanContext *s) {
408 }
409
410 static int xan_decode_frame(AVCodecContext *avctx,
411                             void *data, int *data_size,
412                             uint8_t *buf, int buf_size)
413 {
414     XanContext *s = avctx->priv_data;
415     AVPaletteControl *palette_control = avctx->palctrl;
416
417     if (avctx->get_buffer(avctx, &s->current_frame)) {
418         av_log(s->avctx, AV_LOG_ERROR, "  Xan Video: get_buffer() failed\n");
419         return -1;
420     }
421     s->current_frame.reference = 3;
422
423     if (!s->frame_size)
424         s->frame_size = s->current_frame.linesize[0] * s->avctx->height;
425
426     palette_control->palette_changed = 0;
427     memcpy(s->current_frame.data[1], palette_control->palette,
428         AVPALETTE_SIZE);
429     s->current_frame.palette_has_changed = 1;
430
431     s->buf = buf;
432     s->size = buf_size;
433
434     if (avctx->codec->id == CODEC_ID_XAN_WC3)
435         xan_wc3_decode_frame(s);
436     else if (avctx->codec->id == CODEC_ID_XAN_WC4)
437         xan_wc4_decode_frame(s);
438
439     /* release the last frame if it is allocated */
440     if (s->last_frame.data[0])
441         avctx->release_buffer(avctx, &s->last_frame);
442
443     /* shuffle frames */
444     s->last_frame = s->current_frame;
445
446     *data_size = sizeof(AVFrame);
447     *(AVFrame*)data = s->current_frame;
448
449     /* always report that the buffer was completely consumed */
450     return buf_size;
451 }
452
453 static int xan_decode_end(AVCodecContext *avctx)
454 {
455     XanContext *s = avctx->priv_data;
456
457     /* release the last frame */
458     if (s->last_frame.data[0])
459         avctx->release_buffer(avctx, &s->last_frame);
460
461     av_free(s->buffer1);
462     av_free(s->buffer2);
463
464     return 0;
465 }
466
467 AVCodec xan_wc3_decoder = {
468     "xan_wc3",
469     CODEC_TYPE_VIDEO,
470     CODEC_ID_XAN_WC3,
471     sizeof(XanContext),
472     xan_decode_init,
473     NULL,
474     xan_decode_end,
475     xan_decode_frame,
476     CODEC_CAP_DR1,
477 };
478
479 /*
480 AVCodec xan_wc4_decoder = {
481     "xan_wc4",
482     CODEC_TYPE_VIDEO,
483     CODEC_ID_XAN_WC4,
484     sizeof(XanContext),
485     xan_decode_init,
486     NULL,
487     xan_decode_end,
488     xan_decode_frame,
489     CODEC_CAP_DR1,
490 };
491 */