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