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