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