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