Statistics
| Branch: | Revision:

ffmpeg / libavcodec / xan.c @ 399cec2f

History | View | Annotate | Download (22.8 KB)

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