various security fixes and precautionary checks
[libav.git] / libavcodec / xan.c
CommitLineData
9937e686
MM
1/*
2 * Wing Commander/Xan Video Decoder
3 * Copyright (C) 2003 the ffmpeg project
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21/**
22 * @file xan.c
23 * Xan video decoder for Wing Commander III & IV computer games
24 * by Mario Brito (mbrito@student.dei.uc.pt)
25 * and Mike Melanson (melanson@pcisys.net)
6f4915ac
MM
26 *
27 * The xan_wc3 decoder outputs the following colorspaces natively:
28 * PAL8 (default), RGB555, RGB565, RGB24, BGR24, RGBA32, YUV444P
9937e686
MM
29 */
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35
36#include "common.h"
37#include "avcodec.h"
38#include "dsputil.h"
39
40#define PALETTE_COUNT 256
41#define PALETTE_CONTROL_SIZE ((256 * 3) + 1)
42
43typedef struct XanContext {
44
45 AVCodecContext *avctx;
46 DSPContext dsp;
47 AVFrame last_frame;
48 AVFrame current_frame;
49
50 unsigned char *buf;
51 int size;
52
53 unsigned char palette[PALETTE_COUNT * 4];
54
55 /* scratch space */
56 unsigned char *buffer1;
57 unsigned char *buffer2;
58
59} XanContext;
60
9937e686
MM
61/* RGB -> YUV conversion stuff */
62#define SCALEFACTOR 65536
63#define CENTERSAMPLE 128
64
65#define COMPUTE_Y(r, g, b) \
66 (unsigned char) \
67 ((y_r_table[r] + y_g_table[g] + y_b_table[b]) / SCALEFACTOR)
68#define COMPUTE_U(r, g, b) \
69 (unsigned char) \
70 ((u_r_table[r] + u_g_table[g] + u_b_table[b]) / SCALEFACTOR + CENTERSAMPLE)
71#define COMPUTE_V(r, g, b) \
72 (unsigned char) \
73 ((v_r_table[r] + v_g_table[g] + v_b_table[b]) / SCALEFACTOR + CENTERSAMPLE)
74
75#define Y_R (SCALEFACTOR * 0.29900)
76#define Y_G (SCALEFACTOR * 0.58700)
77#define Y_B (SCALEFACTOR * 0.11400)
78
79#define U_R (SCALEFACTOR * -0.16874)
80#define U_G (SCALEFACTOR * -0.33126)
81#define U_B (SCALEFACTOR * 0.50000)
82
83#define V_R (SCALEFACTOR * 0.50000)
84#define V_G (SCALEFACTOR * -0.41869)
85#define V_B (SCALEFACTOR * -0.08131)
86
87/*
88 * Precalculate all of the YUV tables since it requires fewer than
89 * 10 kilobytes to store them.
90 */
91static int y_r_table[256];
92static int y_g_table[256];
93static int y_b_table[256];
94
95static int u_r_table[256];
96static int u_g_table[256];
97static int u_b_table[256];
98
99static int v_r_table[256];
100static int v_g_table[256];
101static int v_b_table[256];
102
103static int xan_decode_init(AVCodecContext *avctx)
104{
105 XanContext *s = avctx->priv_data;
106 int i;
107
108 s->avctx = avctx;
109
110 if ((avctx->codec->id == CODEC_ID_XAN_WC3) &&
2a2bbcb0 111 (s->avctx->palctrl == NULL)) {
9b879566 112 av_log(avctx, AV_LOG_ERROR, " WC3 Xan video: palette expected.\n");
9937e686
MM
113 return -1;
114 }
115
6f4915ac 116 avctx->pix_fmt = PIX_FMT_PAL8;
9937e686
MM
117 avctx->has_b_frames = 0;
118 dsputil_init(&s->dsp, avctx);
119
120 /* initialize the RGB -> YUV tables */
121 for (i = 0; i < 256; i++) {
122 y_r_table[i] = Y_R * i;
123 y_g_table[i] = Y_G * i;
124 y_b_table[i] = Y_B * i;
125
126 u_r_table[i] = U_R * i;
127 u_g_table[i] = U_G * i;
128 u_b_table[i] = U_B * i;
129
130 v_r_table[i] = V_R * i;
131 v_g_table[i] = V_G * i;
132 v_b_table[i] = V_B * i;
133 }
134
0ecca7a4
MN
135 if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
136 return -1;
137
6f4915ac
MM
138 s->buffer1 = av_malloc(avctx->width * avctx->height);
139 s->buffer2 = av_malloc(avctx->width * avctx->height);
9937e686
MM
140 if (!s->buffer1 || !s->buffer2)
141 return -1;
142
143 return 0;
144}
145
146/* This function is used in lieu of memcpy(). This decoder can not use
147 * memcpy because the memory locations often overlap and
148 * memcpy doesn't like that; it's not uncommon, for example, for
149 * dest = src+1, to turn byte A into pattern AAAAAAAA.
150 * This was originally repz movsb in Intel x86 ASM. */
151static inline void bytecopy(unsigned char *dest, unsigned char *src, int count)
152{
153 int i;
154
155 for (i = 0; i < count; i++)
156 dest[i] = src[i];
157}
158
6f4915ac 159static int xan_huffman_decode(unsigned char *dest, unsigned char *src)
9937e686
MM
160{
161 unsigned char byte = *src++;
162 unsigned char ival = byte + 0x16;
163 unsigned char * ptr = src + byte*2;
164 unsigned char val = ival;
165 int counter = 0;
166
167 unsigned char bits = *ptr++;
168
169 while ( val != 0x16 ) {
170 if ( (1 << counter) & bits )
171 val = src[byte + val - 0x17];
172 else
173 val = src[val - 0x17];
174
175 if ( val < 0x16 ) {
176 *dest++ = val;
177 val = ival;
178 }
179
180 if (counter++ == 7) {
181 counter = 0;
182 bits = *ptr++;
183 }
184 }
185
186 return 0;
187}
188
6f4915ac 189static void xan_unpack(unsigned char *dest, unsigned char *src)
9937e686
MM
190{
191 unsigned char opcode;
192 int size;
193 int offset;
194 int byte1, byte2, byte3;
195
196 for (;;) {
197 opcode = *src++;
198
199 if ( (opcode & 0x80) == 0 ) {
200
201 offset = *src++;
202
203 size = opcode & 3;
204 bytecopy(dest, src, size); dest += size; src += size;
205
206 size = ((opcode & 0x1c) >> 2) + 3;
207 bytecopy (dest, dest - (((opcode & 0x60) << 3) + offset + 1), size);
208 dest += size;
209
210 } else if ( (opcode & 0x40) == 0 ) {
211
212 byte1 = *src++;
213 byte2 = *src++;
214
215 size = byte1 >> 6;
216 bytecopy (dest, src, size); dest += size; src += size;
217
218 size = (opcode & 0x3f) + 4;
219 bytecopy (dest, dest - (((byte1 & 0x3f) << 8) + byte2 + 1), size);
220 dest += size;
221
222 } else if ( (opcode & 0x20) == 0 ) {
223
224 byte1 = *src++;
225 byte2 = *src++;
226 byte3 = *src++;
227
228 size = opcode & 3;
229 bytecopy (dest, src, size); dest += size; src += size;
230
231 size = byte3 + 5 + ((opcode & 0xc) << 6);
232 bytecopy (dest,
233 dest - ((((opcode & 0x10) >> 4) << 0x10) + 1 + (byte1 << 8) + byte2),
234 size);
235 dest += size;
236 } else {
237 size = ((opcode & 0x1f) << 2) + 4;
238
239 if (size > 0x70)
240 break;
241
242 bytecopy (dest, src, size); dest += size; src += size;
243 }
244 }
245
246 size = opcode & 3;
247 bytecopy(dest, src, size); dest += size; src += size;
9937e686
MM
248}
249
250static void inline xan_wc3_build_palette(XanContext *s,
2a2bbcb0 251 unsigned int *palette_data)
9937e686
MM
252{
253 int i;
254 unsigned char r, g, b;
6f4915ac
MM
255 unsigned short *palette16;
256 unsigned int *palette32;
2a2bbcb0 257 unsigned int pal_elem;
9937e686
MM
258
259 /* transform the palette passed through the palette control structure
260 * into the necessary internal format depending on colorspace */
261
262 switch (s->avctx->pix_fmt) {
263
6f4915ac
MM
264 case PIX_FMT_RGB555:
265 palette16 = (unsigned short *)s->palette;
266 for (i = 0; i < PALETTE_COUNT; i++) {
2a2bbcb0
MM
267 pal_elem = palette_data[i];
268 r = (pal_elem >> 16) & 0xff;
269 g = (pal_elem >> 8) & 0xff;
270 b = pal_elem & 0xff;
6f4915ac
MM
271 palette16[i] =
272 ((r >> 3) << 10) |
273 ((g >> 3) << 5) |
42e96409 274 ((b >> 3) << 0);
6f4915ac
MM
275 }
276 break;
277
278 case PIX_FMT_RGB565:
279 palette16 = (unsigned short *)s->palette;
280 for (i = 0; i < PALETTE_COUNT; i++) {
2a2bbcb0
MM
281 pal_elem = palette_data[i];
282 r = (pal_elem >> 16) & 0xff;
283 g = (pal_elem >> 8) & 0xff;
284 b = pal_elem & 0xff;
6f4915ac
MM
285 palette16[i] =
286 ((r >> 3) << 11) |
287 ((g >> 2) << 5) |
42e96409 288 ((b >> 3) << 0);
6f4915ac
MM
289 }
290 break;
291
292 case PIX_FMT_RGB24:
293 for (i = 0; i < PALETTE_COUNT; i++) {
2a2bbcb0
MM
294 pal_elem = palette_data[i];
295 r = (pal_elem >> 16) & 0xff;
296 g = (pal_elem >> 8) & 0xff;
297 b = pal_elem & 0xff;
298 s->palette[i * 4 + 0] = r;
299 s->palette[i * 4 + 1] = g;
300 s->palette[i * 4 + 2] = b;
6f4915ac
MM
301 }
302 break;
303
304 case PIX_FMT_BGR24:
305 for (i = 0; i < PALETTE_COUNT; i++) {
2a2bbcb0
MM
306 pal_elem = palette_data[i];
307 r = (pal_elem >> 16) & 0xff;
308 g = (pal_elem >> 8) & 0xff;
309 b = pal_elem & 0xff;
6f4915ac
MM
310 s->palette[i * 4 + 0] = b;
311 s->palette[i * 4 + 1] = g;
312 s->palette[i * 4 + 2] = r;
313 }
314 break;
315
0f371e3e 316 case PIX_FMT_PAL8:
6f4915ac
MM
317 case PIX_FMT_RGBA32:
318 palette32 = (unsigned int *)s->palette;
2a2bbcb0 319 memcpy (palette32, palette_data, PALETTE_COUNT * sizeof(unsigned int));
6f4915ac
MM
320 break;
321
9937e686
MM
322 case PIX_FMT_YUV444P:
323 for (i = 0; i < PALETTE_COUNT; i++) {
2a2bbcb0
MM
324 pal_elem = palette_data[i];
325 r = (pal_elem >> 16) & 0xff;
326 g = (pal_elem >> 8) & 0xff;
327 b = pal_elem & 0xff;
9937e686
MM
328 s->palette[i * 4 + 0] = COMPUTE_Y(r, g, b);
329 s->palette[i * 4 + 1] = COMPUTE_U(r, g, b);
330 s->palette[i * 4 + 2] = COMPUTE_V(r, g, b);
331 }
332 break;
333
334 default:
9b879566 335 av_log(s->avctx, AV_LOG_ERROR, " Xan WC3: Unhandled colorspace\n");
9937e686
MM
336 break;
337 }
338}
339
42e96409
MM
340/* advance current_x variable; reset accounting variables if current_x
341 * moves beyond width */
342#define ADVANCE_CURRENT_X() \
343 current_x++; \
344 if (current_x >= width) { \
345 index += line_inc; \
346 current_x = 0; \
347 }
348
9937e686
MM
349static void inline xan_wc3_output_pixel_run(XanContext *s,
350 unsigned char *pixel_buffer, int x, int y, int pixel_count)
351{
352 int stride;
353 int line_inc;
354 int index;
355 int current_x;
356 int width = s->avctx->width;
afb8d3f8 357 unsigned char pix;
6f4915ac 358 unsigned char *palette_plane;
9937e686
MM
359 unsigned char *y_plane;
360 unsigned char *u_plane;
361 unsigned char *v_plane;
6f4915ac
MM
362 unsigned char *rgb_plane;
363 unsigned short *rgb16_plane;
364 unsigned short *palette16;
365 unsigned int *rgb32_plane;
366 unsigned int *palette32;
9937e686
MM
367
368 switch (s->avctx->pix_fmt) {
369
6f4915ac
MM
370 case PIX_FMT_PAL8:
371 palette_plane = s->current_frame.data[0];
372 stride = s->current_frame.linesize[0];
373 line_inc = stride - width;
374 index = y * stride + x;
375 current_x = x;
376 while(pixel_count--) {
377
378 /* don't do a memcpy() here; keyframes generally copy an entire
379 * frame of data and the stride needs to be accounted for */
380 palette_plane[index++] = *pixel_buffer++;
381
42e96409 382 ADVANCE_CURRENT_X();
6f4915ac
MM
383 }
384 break;
385
386 case PIX_FMT_RGB555:
387 case PIX_FMT_RGB565:
388 rgb16_plane = (unsigned short *)s->current_frame.data[0];
389 palette16 = (unsigned short *)s->palette;
390 stride = s->current_frame.linesize[0] / 2;
391 line_inc = stride - width;
392 index = y * stride + x;
393 current_x = x;
394 while(pixel_count--) {
395
396 rgb16_plane[index++] = palette16[*pixel_buffer++];
397
42e96409 398 ADVANCE_CURRENT_X();
6f4915ac
MM
399 }
400 break;
401
402 case PIX_FMT_RGB24:
403 case PIX_FMT_BGR24:
404 rgb_plane = s->current_frame.data[0];
405 stride = s->current_frame.linesize[0];
406 line_inc = stride - width * 3;
407 index = y * stride + x * 3;
408 current_x = x;
409 while(pixel_count--) {
afb8d3f8 410 pix = *pixel_buffer++;
6f4915ac 411
afb8d3f8
MM
412 rgb_plane[index++] = s->palette[pix * 4 + 0];
413 rgb_plane[index++] = s->palette[pix * 4 + 1];
414 rgb_plane[index++] = s->palette[pix * 4 + 2];
6f4915ac 415
42e96409 416 ADVANCE_CURRENT_X();
6f4915ac
MM
417 }
418 break;
419
420 case PIX_FMT_RGBA32:
421 rgb32_plane = (unsigned int *)s->current_frame.data[0];
422 palette32 = (unsigned int *)s->palette;
423 stride = s->current_frame.linesize[0] / 4;
424 line_inc = stride - width;
425 index = y * stride + x;
426 current_x = x;
427 while(pixel_count--) {
428
429 rgb32_plane[index++] = palette32[*pixel_buffer++];
430
42e96409 431 ADVANCE_CURRENT_X();
6f4915ac
MM
432 }
433 break;
434
9937e686
MM
435 case PIX_FMT_YUV444P:
436 y_plane = s->current_frame.data[0];
437 u_plane = s->current_frame.data[1];
438 v_plane = s->current_frame.data[2];
439 stride = s->current_frame.linesize[0];
440 line_inc = stride - width;
441 index = y * stride + x;
442 current_x = x;
443 while(pixel_count--) {
afb8d3f8 444 pix = *pixel_buffer++;
9937e686 445
afb8d3f8
MM
446 y_plane[index] = s->palette[pix * 4 + 0];
447 u_plane[index] = s->palette[pix * 4 + 1];
448 v_plane[index] = s->palette[pix * 4 + 2];
9937e686
MM
449
450 index++;
42e96409 451 ADVANCE_CURRENT_X();
9937e686
MM
452 }
453 break;
454
455 default:
9b879566 456 av_log(s->avctx, AV_LOG_ERROR, " Xan WC3: Unhandled colorspace\n");
9937e686
MM
457 break;
458 }
459}
460
42e96409
MM
461#define ADVANCE_CURFRAME_X() \
462 curframe_x++; \
463 if (curframe_x >= width) { \
464 curframe_index += line_inc; \
465 curframe_x = 0; \
466 }
467
468#define ADVANCE_PREVFRAME_X() \
469 prevframe_x++; \
470 if (prevframe_x >= width) { \
471 prevframe_index += line_inc; \
472 prevframe_x = 0; \
473 }
474
9937e686
MM
475static void inline xan_wc3_copy_pixel_run(XanContext *s,
476 int x, int y, int pixel_count, int motion_x, int motion_y)
477{
478 int stride;
479 int line_inc;
480 int curframe_index, prevframe_index;
481 int curframe_x, prevframe_x;
482 int width = s->avctx->width;
6f4915ac 483 unsigned char *palette_plane, *prev_palette_plane;
9937e686
MM
484 unsigned char *y_plane, *u_plane, *v_plane;
485 unsigned char *prev_y_plane, *prev_u_plane, *prev_v_plane;
6f4915ac
MM
486 unsigned char *rgb_plane, *prev_rgb_plane;
487 unsigned short *rgb16_plane, *prev_rgb16_plane;
488 unsigned int *rgb32_plane, *prev_rgb32_plane;
9937e686
MM
489
490 switch (s->avctx->pix_fmt) {
491
6f4915ac
MM
492 case PIX_FMT_PAL8:
493 palette_plane = s->current_frame.data[0];
494 prev_palette_plane = s->last_frame.data[0];
495 stride = s->current_frame.linesize[0];
496 line_inc = stride - width;
497 curframe_index = y * stride + x;
498 curframe_x = x;
499 prevframe_index = (y + motion_y) * stride + x + motion_x;
500 prevframe_x = x + motion_x;
501 while(pixel_count--) {
502
503 palette_plane[curframe_index++] =
504 prev_palette_plane[prevframe_index++];
505
42e96409
MM
506 ADVANCE_CURFRAME_X();
507 ADVANCE_PREVFRAME_X();
6f4915ac
MM
508 }
509 break;
510
511 case PIX_FMT_RGB555:
512 case PIX_FMT_RGB565:
513 rgb16_plane = (unsigned short *)s->current_frame.data[0];
514 prev_rgb16_plane = (unsigned short *)s->last_frame.data[0];
515 stride = s->current_frame.linesize[0] / 2;
516 line_inc = stride - width;
517 curframe_index = y * stride + x;
518 curframe_x = x;
519 prevframe_index = (y + motion_y) * stride + x + motion_x;
520 prevframe_x = x + motion_x;
521 while(pixel_count--) {
522
523 rgb16_plane[curframe_index++] =
524 prev_rgb16_plane[prevframe_index++];
525
42e96409
MM
526 ADVANCE_CURFRAME_X();
527 ADVANCE_PREVFRAME_X();
6f4915ac
MM
528 }
529 break;
530
531 case PIX_FMT_RGB24:
532 case PIX_FMT_BGR24:
533 rgb_plane = s->current_frame.data[0];
534 prev_rgb_plane = s->last_frame.data[0];
535 stride = s->current_frame.linesize[0];
536 line_inc = stride - width * 3;
537 curframe_index = y * stride + x * 3;
538 curframe_x = x;
539 prevframe_index = (y + motion_y) * stride +
540 (3 * (x + motion_x));
541 prevframe_x = x + motion_x;
542 while(pixel_count--) {
543
544 rgb_plane[curframe_index++] = prev_rgb_plane[prevframe_index++];
545 rgb_plane[curframe_index++] = prev_rgb_plane[prevframe_index++];
546 rgb_plane[curframe_index++] = prev_rgb_plane[prevframe_index++];
547
42e96409
MM
548 ADVANCE_CURFRAME_X();
549 ADVANCE_PREVFRAME_X();
6f4915ac
MM
550 }
551 break;
552
553 case PIX_FMT_RGBA32:
554 rgb32_plane = (unsigned int *)s->current_frame.data[0];
555 prev_rgb32_plane = (unsigned int *)s->last_frame.data[0];
556 stride = s->current_frame.linesize[0] / 4;
557 line_inc = stride - width;
558 curframe_index = y * stride + x;
559 curframe_x = x;
560 prevframe_index = (y + motion_y) * stride + x + motion_x;
561 prevframe_x = x + motion_x;
562 while(pixel_count--) {
563
564 rgb32_plane[curframe_index++] =
565 prev_rgb32_plane[prevframe_index++];
566
42e96409
MM
567 ADVANCE_CURFRAME_X();
568 ADVANCE_PREVFRAME_X();
6f4915ac
MM
569 }
570 break;
571
9937e686
MM
572 case PIX_FMT_YUV444P:
573 y_plane = s->current_frame.data[0];
574 u_plane = s->current_frame.data[1];
575 v_plane = s->current_frame.data[2];
576 prev_y_plane = s->last_frame.data[0];
577 prev_u_plane = s->last_frame.data[1];
578 prev_v_plane = s->last_frame.data[2];
579 stride = s->current_frame.linesize[0];
580 line_inc = stride - width;
581 curframe_index = y * stride + x;
582 curframe_x = x;
6f4915ac 583 prevframe_index = (y + motion_y) * stride + x + motion_x;
9937e686
MM
584 prevframe_x = x + motion_x;
585 while(pixel_count--) {
586
587 y_plane[curframe_index] = prev_y_plane[prevframe_index];
588 u_plane[curframe_index] = prev_u_plane[prevframe_index];
589 v_plane[curframe_index] = prev_v_plane[prevframe_index];
590
591 curframe_index++;
42e96409 592 ADVANCE_CURFRAME_X();
9937e686 593 prevframe_index++;
42e96409 594 ADVANCE_PREVFRAME_X();
9937e686
MM
595 }
596 break;
597
598 default:
9b879566 599 av_log(s->avctx, AV_LOG_ERROR, " Xan WC3: Unhandled colorspace\n");
9937e686
MM
600 break;
601 }
602}
603
604static void xan_wc3_decode_frame(XanContext *s) {
605
606 int width = s->avctx->width;
607 int height = s->avctx->height;
608 int total_pixels = width * height;
609 unsigned char opcode;
610 unsigned char flag = 0;
611 int size = 0;
612 int motion_x, motion_y;
613 int x, y;
614
6f4915ac
MM
615 unsigned char *opcode_buffer = s->buffer1;
616 unsigned char *imagedata_buffer = s->buffer2;
9937e686
MM
617
618 /* pointers to segments inside the compressed chunk */
6f4915ac 619 unsigned char *huffman_segment;
9937e686
MM
620 unsigned char *size_segment;
621 unsigned char *vector_segment;
6f4915ac
MM
622 unsigned char *imagedata_segment;
623
624 huffman_segment = s->buf + LE_16(&s->buf[0]);
625 size_segment = s->buf + LE_16(&s->buf[2]);
626 vector_segment = s->buf + LE_16(&s->buf[4]);
627 imagedata_segment = s->buf + LE_16(&s->buf[6]);
9937e686 628
6f4915ac 629 xan_huffman_decode(opcode_buffer, huffman_segment);
9937e686 630
6f4915ac
MM
631 if (imagedata_segment[0] == 2)
632 xan_unpack(imagedata_buffer, &imagedata_segment[1]);
9937e686 633 else
6f4915ac 634 imagedata_buffer = &imagedata_segment[1];
9937e686
MM
635
636 /* use the decoded data segments to build the frame */
637 x = y = 0;
638 while (total_pixels) {
639
6f4915ac 640 opcode = *opcode_buffer++;
9937e686
MM
641 size = 0;
642
643 switch (opcode) {
644
645 case 0:
646 flag ^= 1;
647 continue;
648
649 case 1:
650 case 2:
651 case 3:
652 case 4:
653 case 5:
654 case 6:
655 case 7:
656 case 8:
657 size = opcode;
658 break;
659
660 case 12:
661 case 13:
662 case 14:
663 case 15:
664 case 16:
665 case 17:
666 case 18:
667 size += (opcode - 10);
668 break;
669
670 case 9:
671 case 19:
672 size = *size_segment++;
673 break;
674
675 case 10:
676 case 20:
677 size = BE_16(&size_segment[0]);
678 size_segment += 2;
679 break;
680
681 case 11:
682 case 21:
683 size = (size_segment[0] << 16) | (size_segment[1] << 8) |
684 size_segment[2];
685 size_segment += 3;
686 break;
687 }
688
689 if (opcode < 12) {
690 flag ^= 1;
691 if (flag) {
692 /* run of (size) pixels is unchanged from last frame */
693 xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
694 } else {
6f4915ac
MM
695 /* output a run of pixels from imagedata_buffer */
696 xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);
697 imagedata_buffer += size;
9937e686
MM
698 }
699 } else {
700 /* run-based motion compensation from last frame */
701 motion_x = (*vector_segment >> 4) & 0xF;
702 motion_y = *vector_segment & 0xF;
703 vector_segment++;
704
705 /* sign extension */
706 if (motion_x & 0x8)
707 motion_x |= 0xFFFFFFF0;
708 if (motion_y & 0x8)
709 motion_y |= 0xFFFFFFF0;
710
711 /* copy a run of pixels from the previous frame */
712 xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
713
714 flag = 0;
715 }
716
717 /* coordinate accounting */
718 total_pixels -= size;
719 while (size) {
720 if (x + size >= width) {
721 y++;
722 size -= (width - x);
723 x = 0;
724 } else {
725 x += size;
726 size = 0;
727 }
728 }
729 }
6f4915ac
MM
730
731 /* for PAL8, make the palette available on the way out */
2a2bbcb0 732 if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
6f4915ac 733 memcpy(s->current_frame.data[1], s->palette, PALETTE_COUNT * 4);
2a2bbcb0
MM
734 s->current_frame.palette_has_changed = 1;
735 s->avctx->palctrl->palette_changed = 0;
736 }
9937e686
MM
737}
738
739static void xan_wc4_decode_frame(XanContext *s) {
740}
741
742static int xan_decode_frame(AVCodecContext *avctx,
743 void *data, int *data_size,
744 uint8_t *buf, int buf_size)
745{
746 XanContext *s = avctx->priv_data;
2a2bbcb0 747 AVPaletteControl *palette_control = avctx->palctrl;
9937e686
MM
748 int keyframe = 0;
749
ba118447 750 if (palette_control->palette_changed) {
9937e686 751 /* load the new palette and reset the palette control */
ba118447 752 xan_wc3_build_palette(s, palette_control->palette);
2a2bbcb0
MM
753 /* If pal8 we clear flag when we copy palette */
754 if (s->avctx->pix_fmt != PIX_FMT_PAL8)
755 palette_control->palette_changed = 0;
9937e686
MM
756 keyframe = 1;
757 }
758
759 if (avctx->get_buffer(avctx, &s->current_frame)) {
9b879566 760 av_log(s->avctx, AV_LOG_ERROR, " Xan Video: get_buffer() failed\n");
9937e686
MM
761 return -1;
762 }
6f4915ac 763 s->current_frame.reference = 3;
9937e686
MM
764
765 s->buf = buf;
766 s->size = buf_size;
767
6f4915ac
MM
768 if (avctx->codec->id == CODEC_ID_XAN_WC3)
769 xan_wc3_decode_frame(s);
770 else if (avctx->codec->id == CODEC_ID_XAN_WC4)
9937e686 771 xan_wc4_decode_frame(s);
6f4915ac 772
9937e686
MM
773 /* release the last frame if it is allocated */
774 if (s->last_frame.data[0])
775 avctx->release_buffer(avctx, &s->last_frame);
776
777 /* shuffle frames */
778 s->last_frame = s->current_frame;
779
780 *data_size = sizeof(AVFrame);
781 *(AVFrame*)data = s->current_frame;
782
783 /* always report that the buffer was completely consumed */
784 return buf_size;
785}
786
787static int xan_decode_end(AVCodecContext *avctx)
788{
789 XanContext *s = avctx->priv_data;
790
791 /* release the last frame */
792 avctx->release_buffer(avctx, &s->last_frame);
793
794 av_free(s->buffer1);
795 av_free(s->buffer2);
796
797 return 0;
798}
799
800AVCodec xan_wc3_decoder = {
801 "xan_wc3",
802 CODEC_TYPE_VIDEO,
803 CODEC_ID_XAN_WC3,
804 sizeof(XanContext),
805 xan_decode_init,
806 NULL,
807 xan_decode_end,
808 xan_decode_frame,
809 CODEC_CAP_DR1,
810};
811
812/*
813AVCodec xan_wc4_decoder = {
814 "xan_wc4",
815 CODEC_TYPE_VIDEO,
816 CODEC_ID_XAN_WC4,
817 sizeof(XanContext),
818 xan_decode_init,
819 NULL,
820 xan_decode_end,
821 xan_decode_frame,
822 CODEC_CAP_DR1,
823};
824*/