Statistics
| Branch: | Revision:

ffmpeg / libavcodec / interplayvideo.c @ 5509bffa

History | View | Annotate | Download (28 KB)

1 3ef8be2b Mike Melanson
/*
2
 * Interplay MVE 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 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 3ef8be2b Mike Melanson
 *
19
 */
20
21
/**
22 186447f8 Mike Melanson
 * @file interplayvideo.c
23
 * Interplay MVE Video Decoder by Mike Melanson (melanson@pcisys.net)
24 3ef8be2b Mike Melanson
 * For more information about the Interplay MVE format, visit:
25 186447f8 Mike Melanson
 *   http://www.pcisys.net/~melanson/codecs/interplay-mve.txt
26
 * This code is written in such a way that the identifiers match up
27
 * with the encoding descriptions in the document.
28
 *
29
 * This decoder presently only supports a PAL8 output colorspace.
30
 *
31
 * An Interplay video frame consists of 2 parts: The decoding map and
32
 * the video data. A demuxer must load these 2 parts together in a single
33
 * buffer before sending it through the stream to this decoder.
34 3ef8be2b Mike Melanson
 */
35
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
#include <unistd.h>
40
41
#include "common.h"
42
#include "avcodec.h"
43
#include "dsputil.h"
44
45 186447f8 Mike Melanson
#define PALETTE_COUNT 256
46
47
/* debugging support */
48
#define DEBUG_INTERPLAY 0
49
#if DEBUG_INTERPLAY
50 58aa2b1d Alex Beregszaszi
#define debug_interplay(x,...) av_log(NULL, AV_LOG_DEBUG, x, __VA_ARGS__)
51 186447f8 Mike Melanson
#else
52
static inline void debug_interplay(const char *format, ...) { }
53
#endif
54
55 3ef8be2b Mike Melanson
typedef struct IpvideoContext {
56
57
    AVCodecContext *avctx;
58
    DSPContext dsp;
59 d86e8568 Mike Melanson
    AVFrame second_last_frame;
60 3ef8be2b Mike Melanson
    AVFrame last_frame;
61
    AVFrame current_frame;
62
    unsigned char *decoding_map;
63
    int decoding_map_size;
64
65
    unsigned char *buf;
66
    int size;
67
68 bf649a9d Mike Melanson
    unsigned char *stream_ptr;
69
    unsigned char *stream_end;
70
    unsigned char *pixel_ptr;
71
    int line_inc;
72
    int stride;
73
    int upper_motion_limit_offset;
74
75 3ef8be2b Mike Melanson
} IpvideoContext;
76
77 186447f8 Mike Melanson
#define CHECK_STREAM_PTR(n) \
78 bf649a9d Mike Melanson
  if ((s->stream_ptr + n) > s->stream_end) { \
79 9b879566 Michel Bardiaux
    av_log(s->avctx, AV_LOG_ERROR, "Interplay video warning: stream_ptr out of bounds (%p >= %p)\n", \
80 bf649a9d Mike Melanson
      s->stream_ptr + n, s->stream_end); \
81 186447f8 Mike Melanson
    return -1; \
82
  }
83
84
#define COPY_FROM_CURRENT() \
85
    motion_offset = current_offset; \
86 bf649a9d Mike Melanson
    motion_offset += y * s->stride; \
87 186447f8 Mike Melanson
    motion_offset += x; \
88
    if (motion_offset < 0) { \
89 9b879566 Michel Bardiaux
        av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset < 0 (%d)\n", motion_offset); \
90 186447f8 Mike Melanson
        return -1; \
91 bf649a9d Mike Melanson
    } else if (motion_offset > s->upper_motion_limit_offset) { \
92 9b879566 Michel Bardiaux
        av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset above limit (%d >= %d)\n", \
93 bf649a9d Mike Melanson
            motion_offset, s->upper_motion_limit_offset); \
94 186447f8 Mike Melanson
        return -1; \
95
    } \
96 bf649a9d Mike Melanson
    s->dsp.put_pixels_tab[0][0](s->pixel_ptr, \
97
        s->current_frame.data[0] + motion_offset, s->stride, 8);
98 186447f8 Mike Melanson
99
#define COPY_FROM_PREVIOUS() \
100
    motion_offset = current_offset; \
101 bf649a9d Mike Melanson
    motion_offset += y * s->stride; \
102 186447f8 Mike Melanson
    motion_offset += x; \
103
    if (motion_offset < 0) { \
104 9b879566 Michel Bardiaux
        av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset < 0 (%d)\n", motion_offset); \
105 186447f8 Mike Melanson
        return -1; \
106 bf649a9d Mike Melanson
    } else if (motion_offset > s->upper_motion_limit_offset) { \
107 9b879566 Michel Bardiaux
        av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset above limit (%d >= %d)\n", \
108 bf649a9d Mike Melanson
            motion_offset, s->upper_motion_limit_offset); \
109 186447f8 Mike Melanson
        return -1; \
110
    } \
111 bf649a9d Mike Melanson
    s->dsp.put_pixels_tab[0][0](s->pixel_ptr, \
112
        s->last_frame.data[0] + motion_offset, s->stride, 8);
113 186447f8 Mike Melanson
114 d86e8568 Mike Melanson
#define COPY_FROM_SECOND_LAST() \
115
    motion_offset = current_offset; \
116 bf649a9d Mike Melanson
    motion_offset += y * s->stride; \
117 d86e8568 Mike Melanson
    motion_offset += x; \
118
    if (motion_offset < 0) { \
119 9b879566 Michel Bardiaux
        av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset < 0 (%d)\n", motion_offset); \
120 d86e8568 Mike Melanson
        return -1; \
121 bf649a9d Mike Melanson
    } else if (motion_offset > s->upper_motion_limit_offset) { \
122 9b879566 Michel Bardiaux
        av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset above limit (%d >= %d)\n", \
123 bf649a9d Mike Melanson
            motion_offset, s->upper_motion_limit_offset); \
124 d86e8568 Mike Melanson
        return -1; \
125
    } \
126 bf649a9d Mike Melanson
    s->dsp.put_pixels_tab[0][0](s->pixel_ptr, \
127
        s->second_last_frame.data[0] + motion_offset, s->stride, 8);
128
129
static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s)
130
{
131
    int x, y;
132
    int motion_offset;
133
    int current_offset = s->pixel_ptr - s->current_frame.data[0];
134
135
    /* copy a block from the previous frame */
136
    x = y = 0;
137
    COPY_FROM_PREVIOUS();
138 d86e8568 Mike Melanson
139 bf649a9d Mike Melanson
    /* report success */
140
    return 0;
141
}
142
143
static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s)
144
{
145
    int x, y;
146
    int motion_offset;
147
    int current_offset = s->pixel_ptr - s->current_frame.data[0];
148
149
    /* copy block from 2 frames ago */
150
    x = y = 0;
151
    COPY_FROM_SECOND_LAST();
152
153
    /* report success */
154
    return 0;
155
}
156
157
static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s)
158 186447f8 Mike Melanson
{
159
    unsigned char B;
160
    int x, y;
161
    int motion_offset;
162 bf649a9d Mike Melanson
    int current_offset = s->pixel_ptr - s->current_frame.data[0];
163 186447f8 Mike Melanson
164 bf649a9d Mike Melanson
    /* copy block from 2 frames ago using a motion vector; need 1 more byte */
165 186447f8 Mike Melanson
    CHECK_STREAM_PTR(1);
166 bf649a9d Mike Melanson
    B = *s->stream_ptr++;
167 186447f8 Mike Melanson
168
    if (B < 56) {
169
        x = 8 + (B % 7);
170
        y = B / 7;
171
    } else {
172
        x = -14 + ((B - 56) % 29);
173
        y =   8 + ((B - 56) / 29);
174
    }
175
176
    debug_interplay ("    motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
177 d86e8568 Mike Melanson
    COPY_FROM_SECOND_LAST();
178 186447f8 Mike Melanson
179
    /* report success */
180
    return 0;
181
}
182
183 bf649a9d Mike Melanson
static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s)
184 186447f8 Mike Melanson
{
185
    unsigned char B;
186
    int x, y;
187
    int motion_offset;
188 bf649a9d Mike Melanson
    int current_offset = s->pixel_ptr - s->current_frame.data[0];
189 186447f8 Mike Melanson
190
    /* copy 8x8 block from current frame from an up/left block */
191
192
    /* need 1 more byte for motion */
193
    CHECK_STREAM_PTR(1);
194 bf649a9d Mike Melanson
    B = *s->stream_ptr++;
195 186447f8 Mike Melanson
196
    if (B < 56) {
197
        x = -(8 + (B % 7));
198
        y = -(B / 7);
199
    } else {
200
        x = -(-14 + ((B - 56) % 29));
201
        y = -(  8 + ((B - 56) / 29));
202
    }
203
204
    debug_interplay ("    motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
205
    COPY_FROM_CURRENT();
206
207
    /* report success */
208
    return 0;
209
}
210
211 bf649a9d Mike Melanson
static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s)
212 186447f8 Mike Melanson
{
213
    int x, y;
214
    unsigned char B, BL, BH;
215
    int motion_offset;
216 bf649a9d Mike Melanson
    int current_offset = s->pixel_ptr - s->current_frame.data[0];
217 186447f8 Mike Melanson
218
    /* copy a block from the previous frame; need 1 more byte */
219
    CHECK_STREAM_PTR(1);
220
221 bf649a9d Mike Melanson
    B = *s->stream_ptr++;
222 186447f8 Mike Melanson
    BL = B & 0x0F;
223
    BH = (B >> 4) & 0x0F;
224
    x = -8 + BL;
225
    y = -8 + BH;
226
227
    debug_interplay ("    motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
228
    COPY_FROM_PREVIOUS();
229
230
    /* report success */
231
    return 0;
232
}
233
234 bf649a9d Mike Melanson
static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s)
235 186447f8 Mike Melanson
{
236
    signed char x, y;
237
    int motion_offset;
238 bf649a9d Mike Melanson
    int current_offset = s->pixel_ptr - s->current_frame.data[0];
239 186447f8 Mike Melanson
240
    /* copy a block from the previous frame using an expanded range;
241
     * need 2 more bytes */
242
    CHECK_STREAM_PTR(2);
243
244 bf649a9d Mike Melanson
    x = *s->stream_ptr++;
245
    y = *s->stream_ptr++;
246 186447f8 Mike Melanson
247
    debug_interplay ("    motion bytes = %d, %d\n", x, y);
248
    COPY_FROM_PREVIOUS();
249
250
    /* report success */
251
    return 0;
252
}
253
254 bf649a9d Mike Melanson
static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s)
255 186447f8 Mike Melanson
{
256
    /* mystery opcode? skip multiple blocks? */
257 9b879566 Michel Bardiaux
    av_log(s->avctx, AV_LOG_ERROR, "  Interplay video: Help! Mystery opcode 0x6 seen\n");
258 186447f8 Mike Melanson
259
    /* report success */
260
    return 0;
261
}
262
263 bf649a9d Mike Melanson
static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s)
264 186447f8 Mike Melanson
{
265
    int x, y;
266
    unsigned char P0, P1;
267
    unsigned char B[8];
268
    unsigned int flags;
269
    int bitmask;
270
271
    /* 2-color encoding */
272
    CHECK_STREAM_PTR(2);
273
274 bf649a9d Mike Melanson
    P0 = *s->stream_ptr++;
275
    P1 = *s->stream_ptr++;
276 186447f8 Mike Melanson
277
    if (P0 <= P1) {
278
279
        /* need 8 more bytes from the stream */
280
        CHECK_STREAM_PTR(8);
281
        for (y = 0; y < 8; y++)
282 bf649a9d Mike Melanson
            B[y] = *s->stream_ptr++;
283 186447f8 Mike Melanson
284
        for (y = 0; y < 8; y++) {
285
            flags = B[y];
286 75816852 Mike Melanson
            for (x = 0x01; x <= 0x80; x <<= 1) {
287 186447f8 Mike Melanson
                if (flags & x)
288 bf649a9d Mike Melanson
                    *s->pixel_ptr++ = P1;
289 186447f8 Mike Melanson
                else
290 bf649a9d Mike Melanson
                    *s->pixel_ptr++ = P0;
291 186447f8 Mike Melanson
            }
292 bf649a9d Mike Melanson
            s->pixel_ptr += s->line_inc;
293 186447f8 Mike Melanson
        }
294
295
    } else {
296
297
        /* need 2 more bytes from the stream */
298
        CHECK_STREAM_PTR(2);
299 bf649a9d Mike Melanson
        B[0] = *s->stream_ptr++;
300
        B[1] = *s->stream_ptr++;
301 186447f8 Mike Melanson
302 75816852 Mike Melanson
        flags = (B[1] << 8) | B[0];
303
        bitmask = 0x0001;
304 186447f8 Mike Melanson
        for (y = 0; y < 8; y += 2) {
305 75816852 Mike Melanson
            for (x = 0; x < 8; x += 2, bitmask <<= 1) {
306 186447f8 Mike Melanson
                if (flags & bitmask) {
307 bf649a9d Mike Melanson
                    *(s->pixel_ptr + x) = P1;
308
                    *(s->pixel_ptr + x + 1) = P1;
309
                    *(s->pixel_ptr + s->stride + x) = P1;
310
                    *(s->pixel_ptr + s->stride + x + 1) = P1;
311 d86e8568 Mike Melanson
                } else {
312 bf649a9d Mike Melanson
                    *(s->pixel_ptr + x) = P0;
313
                    *(s->pixel_ptr + x + 1) = P0;
314
                    *(s->pixel_ptr + s->stride + x) = P0;
315
                    *(s->pixel_ptr + s->stride + x + 1) = P0;
316 186447f8 Mike Melanson
                }
317
            }
318 bf649a9d Mike Melanson
            s->pixel_ptr += s->stride * 2;
319 186447f8 Mike Melanson
        }
320
    }
321
322
    /* report success */
323
    return 0;
324
}
325
326 bf649a9d Mike Melanson
static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s)
327 186447f8 Mike Melanson
{
328
    int x, y;
329
    unsigned char P[8];
330
    unsigned char B[8];
331
    unsigned int flags = 0;
332
    unsigned int bitmask = 0;
333
    unsigned char P0 = 0, P1 = 0;
334
    int lower_half = 0;
335
336
    /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
337
     * either top and bottom or left and right halves */
338
    CHECK_STREAM_PTR(2);
339
340 bf649a9d Mike Melanson
    P[0] = *s->stream_ptr++;
341
    P[1] = *s->stream_ptr++;
342 186447f8 Mike Melanson
343
    if (P[0] <= P[1]) {
344
345
        /* need 12 more bytes */
346
        CHECK_STREAM_PTR(12);
347 bf649a9d Mike Melanson
        B[0] = *s->stream_ptr++;  B[1] = *s->stream_ptr++;
348
        P[2] = *s->stream_ptr++;  P[3] = *s->stream_ptr++;
349
        B[2] = *s->stream_ptr++;  B[3] = *s->stream_ptr++;
350
        P[4] = *s->stream_ptr++;  P[5] = *s->stream_ptr++;
351
        B[4] = *s->stream_ptr++;  B[5] = *s->stream_ptr++;
352
        P[6] = *s->stream_ptr++;  P[7] = *s->stream_ptr++;
353
        B[6] = *s->stream_ptr++;  B[7] = *s->stream_ptr++;
354 186447f8 Mike Melanson
355
        for (y = 0; y < 8; y++) {
356
357
            /* time to reload flags? */
358
            if (y == 0) {
359
                flags =
360 75816852 Mike Melanson
                    ((B[0] & 0xF0) <<  4) | ((B[4] & 0xF0) <<  8) |
361
                    ((B[0] & 0x0F)      ) | ((B[4] & 0x0F) <<  4) |
362
                    ((B[1] & 0xF0) << 20) | ((B[5] & 0xF0) << 24) |
363
                    ((B[1] & 0x0F) << 16) | ((B[5] & 0x0F) << 20);
364
                bitmask = 0x00000001;
365 186447f8 Mike Melanson
                lower_half = 0;  /* still on top half */
366
            } else if (y == 4) {
367
                flags =
368 75816852 Mike Melanson
                    ((B[2] & 0xF0) <<  4) | ((B[6] & 0xF0) <<  8) |
369
                    ((B[2] & 0x0F)      ) | ((B[6] & 0x0F) <<  4) |
370
                    ((B[3] & 0xF0) << 20) | ((B[7] & 0xF0) << 24) |
371
                    ((B[3] & 0x0F) << 16) | ((B[7] & 0x0F) << 20);
372
                bitmask = 0x00000001;
373 d86e8568 Mike Melanson
                lower_half = 2;
374 186447f8 Mike Melanson
            }
375
376 75816852 Mike Melanson
            for (x = 0; x < 8; x++, bitmask <<= 1) {
377 186447f8 Mike Melanson
                /* get the pixel values ready for this quadrant */
378
                if (x == 0) {
379
                    P0 = P[lower_half + 0];
380
                    P1 = P[lower_half + 1];
381
                } else if (x == 4) {
382 d86e8568 Mike Melanson
                    P0 = P[lower_half + 4];
383
                    P1 = P[lower_half + 5];
384 186447f8 Mike Melanson
                }
385
386
                if (flags & bitmask)
387 bf649a9d Mike Melanson
                    *s->pixel_ptr++ = P1;
388 186447f8 Mike Melanson
                else
389 bf649a9d Mike Melanson
                    *s->pixel_ptr++ = P0;
390 186447f8 Mike Melanson
            }
391 bf649a9d Mike Melanson
            s->pixel_ptr += s->line_inc;
392 186447f8 Mike Melanson
        }
393
394
    } else {
395
396
        /* need 10 more bytes */
397
        CHECK_STREAM_PTR(10);
398 bf649a9d Mike Melanson
        B[0] = *s->stream_ptr++;  B[1] = *s->stream_ptr++;
399
        B[2] = *s->stream_ptr++;  B[3] = *s->stream_ptr++;
400
        P[2] = *s->stream_ptr++;  P[3] = *s->stream_ptr++;
401
        B[4] = *s->stream_ptr++;  B[5] = *s->stream_ptr++;
402
        B[6] = *s->stream_ptr++;  B[7] = *s->stream_ptr++;
403 186447f8 Mike Melanson
404
        if (P[2] <= P[3]) {
405
406
            /* vertical split; left & right halves are 2-color encoded */
407
408
            for (y = 0; y < 8; y++) {
409
410
                /* time to reload flags? */
411
                if (y == 0) {
412
                    flags =
413 75816852 Mike Melanson
                        ((B[0] & 0xF0) <<  4) | ((B[4] & 0xF0) <<  8) |
414
                        ((B[0] & 0x0F)      ) | ((B[4] & 0x0F) <<  4) |
415
                        ((B[1] & 0xF0) << 20) | ((B[5] & 0xF0) << 24) |
416
                        ((B[1] & 0x0F) << 16) | ((B[5] & 0x0F) << 20);
417
                    bitmask = 0x00000001;
418 186447f8 Mike Melanson
                } else if (y == 4) {
419
                    flags =
420 75816852 Mike Melanson
                        ((B[2] & 0xF0) <<  4) | ((B[6] & 0xF0) <<  8) |
421
                        ((B[2] & 0x0F)      ) | ((B[6] & 0x0F) <<  4) |
422
                        ((B[3] & 0xF0) << 20) | ((B[7] & 0xF0) << 24) |
423
                        ((B[3] & 0x0F) << 16) | ((B[7] & 0x0F) << 20);
424
                    bitmask = 0x00000001;
425 186447f8 Mike Melanson
                }
426
427 75816852 Mike Melanson
                for (x = 0; x < 8; x++, bitmask <<= 1) {
428 186447f8 Mike Melanson
                    /* get the pixel values ready for this half */
429
                    if (x == 0) {
430
                        P0 = P[0];
431
                        P1 = P[1];
432
                    } else if (x == 4) {
433
                        P0 = P[2];
434
                        P1 = P[3];
435
                    }
436
437
                    if (flags & bitmask)
438 bf649a9d Mike Melanson
                        *s->pixel_ptr++ = P1;
439 d86e8568 Mike Melanson
                    else
440 bf649a9d Mike Melanson
                        *s->pixel_ptr++ = P0;
441 186447f8 Mike Melanson
                }
442 bf649a9d Mike Melanson
                s->pixel_ptr += s->line_inc;
443 186447f8 Mike Melanson
            }
444
445
        } else {
446
447
            /* horizontal split; top & bottom halves are 2-color encoded */
448
449
            for (y = 0; y < 8; y++) {
450
451
                flags = B[y];
452
                if (y == 0) {
453
                    P0 = P[0];
454
                    P1 = P[1];
455
                } else if (y == 4) {
456
                    P0 = P[2];
457
                    P1 = P[3];
458
                }
459
460 75816852 Mike Melanson
                for (bitmask = 0x01; bitmask <= 0x80; bitmask <<= 1) {
461 186447f8 Mike Melanson
462
                    if (flags & bitmask)
463 bf649a9d Mike Melanson
                        *s->pixel_ptr++ = P1;
464 d86e8568 Mike Melanson
                    else
465 bf649a9d Mike Melanson
                        *s->pixel_ptr++ = P0;
466 186447f8 Mike Melanson
                }
467 bf649a9d Mike Melanson
                s->pixel_ptr += s->line_inc;
468 186447f8 Mike Melanson
            }
469
        }
470
    }
471
472
    /* report success */
473
    return 0;
474
}
475
476 bf649a9d Mike Melanson
static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
477 186447f8 Mike Melanson
{
478
    int x, y;
479
    unsigned char P[4];
480 75816852 Mike Melanson
    unsigned char B[4];
481 186447f8 Mike Melanson
    unsigned int flags = 0;
482
    int shifter = 0;
483
    unsigned char pix;
484
485
    /* 4-color encoding */
486
    CHECK_STREAM_PTR(4);
487
488
    for (y = 0; y < 4; y++)
489 bf649a9d Mike Melanson
        P[y] = *s->stream_ptr++;
490 186447f8 Mike Melanson
491
    if ((P[0] <= P[1]) && (P[2] <= P[3])) {
492
493
        /* 1 of 4 colors for each pixel, need 16 more bytes */
494
        CHECK_STREAM_PTR(16);
495
496
        for (y = 0; y < 8; y++) {
497
            /* get the next set of 8 2-bit flags */
498 bf649a9d Mike Melanson
            flags = (s->stream_ptr[1] << 8) | s->stream_ptr[0];
499
            s->stream_ptr += 2;
500 75816852 Mike Melanson
            for (x = 0, shifter = 0; x < 8; x++, shifter += 2) {
501 bf649a9d Mike Melanson
                *s->pixel_ptr++ = P[(flags >> shifter) & 0x03];
502 186447f8 Mike Melanson
            }
503 bf649a9d Mike Melanson
            s->pixel_ptr += s->line_inc;
504 186447f8 Mike Melanson
        }
505
506
    } else if ((P[0] <= P[1]) && (P[2] > P[3])) {
507
508
        /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
509
        CHECK_STREAM_PTR(4);
510
511 bf649a9d Mike Melanson
        B[0] = *s->stream_ptr++;
512
        B[1] = *s->stream_ptr++;
513
        B[2] = *s->stream_ptr++;
514
        B[3] = *s->stream_ptr++;
515 75816852 Mike Melanson
        flags = (B[3] << 24) | (B[2] << 16) | (B[1] << 8) | B[0];
516
        shifter = 0;
517 186447f8 Mike Melanson
518
        for (y = 0; y < 8; y += 2) {
519 75816852 Mike Melanson
            for (x = 0; x < 8; x += 2, shifter += 2) {
520 186447f8 Mike Melanson
                pix = P[(flags >> shifter) & 0x03];
521 bf649a9d Mike Melanson
                *(s->pixel_ptr + x) = pix;
522
                *(s->pixel_ptr + x + 1) = pix;
523
                *(s->pixel_ptr + s->stride + x) = pix;
524
                *(s->pixel_ptr + s->stride + x + 1) = pix;
525 186447f8 Mike Melanson
            }
526 bf649a9d Mike Melanson
            s->pixel_ptr += s->stride * 2;
527 186447f8 Mike Melanson
        }
528
529
    } else if ((P[0] > P[1]) && (P[2] <= P[3])) {
530
531
        /* 1 of 4 colors for each 2x1 block, need 8 more bytes */
532
        CHECK_STREAM_PTR(8);
533
534
        for (y = 0; y < 8; y++) {
535
            /* time to reload flags? */
536
            if ((y == 0) || (y == 4)) {
537 bf649a9d Mike Melanson
                B[0] = *s->stream_ptr++;
538
                B[1] = *s->stream_ptr++;
539
                B[2] = *s->stream_ptr++;
540
                B[3] = *s->stream_ptr++;
541 75816852 Mike Melanson
                flags = (B[3] << 24) | (B[2] << 16) | (B[1] << 8) | B[0];
542
                shifter = 0;
543 186447f8 Mike Melanson
            }
544 75816852 Mike Melanson
            for (x = 0; x < 8; x += 2, shifter += 2) {
545 186447f8 Mike Melanson
                pix = P[(flags >> shifter) & 0x03];
546 bf649a9d Mike Melanson
                *(s->pixel_ptr + x) = pix;
547
                *(s->pixel_ptr + x + 1) = pix;
548 186447f8 Mike Melanson
            }
549 bf649a9d Mike Melanson
            s->pixel_ptr += s->stride;
550 186447f8 Mike Melanson
        }
551
552
    } else {
553
554
        /* 1 of 4 colors for each 1x2 block, need 8 more bytes */
555
        CHECK_STREAM_PTR(8);
556
557
        for (y = 0; y < 8; y += 2) {
558
            /* time to reload flags? */
559
            if ((y == 0) || (y == 4)) {
560 bf649a9d Mike Melanson
                B[0] = *s->stream_ptr++;
561
                B[1] = *s->stream_ptr++;
562
                B[2] = *s->stream_ptr++;
563
                B[3] = *s->stream_ptr++;
564 75816852 Mike Melanson
                flags = (B[3] << 24) | (B[2] << 16) | (B[1] << 8) | B[0];
565
                shifter = 0;
566 186447f8 Mike Melanson
            }
567 75816852 Mike Melanson
            for (x = 0; x < 8; x++, shifter += 2) {
568 186447f8 Mike Melanson
                pix = P[(flags >> shifter) & 0x03];
569 bf649a9d Mike Melanson
                *(s->pixel_ptr + x) = pix;
570
                *(s->pixel_ptr + s->stride + x) = pix;
571 186447f8 Mike Melanson
            }
572 bf649a9d Mike Melanson
            s->pixel_ptr += s->stride * 2;
573 186447f8 Mike Melanson
        }
574
    }
575
576
    /* report success */
577
    return 0;
578
}
579
580 bf649a9d Mike Melanson
static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
581 186447f8 Mike Melanson
{
582
    int x, y;
583
    unsigned char P[16];
584
    unsigned char B[16];
585
    int flags = 0;
586
    int shifter = 0;
587
    int index;
588
    int split;
589
    int lower_half;
590
591
    /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
592
     * either top and bottom or left and right halves */
593
    CHECK_STREAM_PTR(4);
594
595
    for (y = 0; y < 4; y++)
596 bf649a9d Mike Melanson
        P[y] = *s->stream_ptr++;
597 186447f8 Mike Melanson
598
    if (P[0] <= P[1]) {
599
600
        /* 4-color encoding for each quadrant; need 28 more bytes */
601
        CHECK_STREAM_PTR(28);
602
603
        for (y = 0; y < 4; y++)
604 bf649a9d Mike Melanson
            B[y] = *s->stream_ptr++;
605 186447f8 Mike Melanson
        for (y = 4; y < 16; y += 4) {
606
            for (x = y; x < y + 4; x++)
607 bf649a9d Mike Melanson
                P[x] = *s->stream_ptr++;
608 186447f8 Mike Melanson
            for (x = y; x < y + 4; x++)
609 bf649a9d Mike Melanson
                B[x] = *s->stream_ptr++;
610 186447f8 Mike Melanson
        }
611
612
        for (y = 0; y < 8; y++) {
613
614
            lower_half = (y >= 4) ? 4 : 0;
615 75816852 Mike Melanson
            flags = (B[y + 8] << 8) | B[y];
616 186447f8 Mike Melanson
617 75816852 Mike Melanson
            for (x = 0, shifter = 0; x < 8; x++, shifter += 2) {
618 186447f8 Mike Melanson
                split = (x >= 4) ? 8 : 0;
619
                index = split + lower_half + ((flags >> shifter) & 0x03);
620 bf649a9d Mike Melanson
                *s->pixel_ptr++ = P[index];
621 186447f8 Mike Melanson
            }
622
623 bf649a9d Mike Melanson
            s->pixel_ptr += s->line_inc;
624 186447f8 Mike Melanson
        }
625
626
    } else {
627
628
        /* 4-color encoding for either left and right or top and bottom
629
         * halves; need 20 more bytes */
630
        CHECK_STREAM_PTR(20);
631
632
        for (y = 0; y < 8; y++)
633 bf649a9d Mike Melanson
            B[y] = *s->stream_ptr++;
634 186447f8 Mike Melanson
        for (y = 4; y < 8; y++)
635 bf649a9d Mike Melanson
            P[y] = *s->stream_ptr++;
636 186447f8 Mike Melanson
        for (y = 8; y < 16; y++)
637 bf649a9d Mike Melanson
            B[y] = *s->stream_ptr++;
638 186447f8 Mike Melanson
639
        if (P[4] <= P[5]) {
640
641
            /* block is divided into left and right halves */
642
            for (y = 0; y < 8; y++) {
643
644 75816852 Mike Melanson
                flags = (B[y + 8] << 8) | B[y];
645 186447f8 Mike Melanson
                split = 0;
646
647 75816852 Mike Melanson
                for (x = 0, shifter = 0; x < 8; x++, shifter += 2) {
648 186447f8 Mike Melanson
                    if (x == 4)
649
                        split = 4;
650 bf649a9d Mike Melanson
                    *s->pixel_ptr++ = P[split + ((flags >> shifter) & 0x03)];
651 186447f8 Mike Melanson
                }
652
653 bf649a9d Mike Melanson
                s->pixel_ptr += s->line_inc;
654 186447f8 Mike Melanson
            }
655
656
        } else {
657
658
            /* block is divided into top and bottom halves */
659
            split = 0;
660
            for (y = 0; y < 8; y++) {
661
662 75816852 Mike Melanson
                flags = (B[y * 2 + 1] << 8) | B[y * 2];
663 186447f8 Mike Melanson
                if (y == 4)
664
                    split = 4;
665
666 75816852 Mike Melanson
                for (x = 0, shifter = 0; x < 8; x++, shifter += 2)
667 bf649a9d Mike Melanson
                    *s->pixel_ptr++ = P[split + ((flags >> shifter) & 0x03)];
668 186447f8 Mike Melanson
669 bf649a9d Mike Melanson
                s->pixel_ptr += s->line_inc;
670 186447f8 Mike Melanson
            }
671
        }
672
    }
673
674
    /* report success */
675
    return 0;
676
}
677
678 bf649a9d Mike Melanson
static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
679 186447f8 Mike Melanson
{
680
    int x, y;
681
682
    /* 64-color encoding (each pixel in block is a different color) */
683
    CHECK_STREAM_PTR(64);
684
685
    for (y = 0; y < 8; y++) {
686
        for (x = 0; x < 8; x++) {
687 bf649a9d Mike Melanson
            *s->pixel_ptr++ = *s->stream_ptr++;
688 186447f8 Mike Melanson
        }
689 bf649a9d Mike Melanson
        s->pixel_ptr += s->line_inc;
690 186447f8 Mike Melanson
    }
691
692
    /* report success */
693
    return 0;
694
}
695
696 bf649a9d Mike Melanson
static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
697 186447f8 Mike Melanson
{
698
    int x, y;
699
    unsigned char pix;
700
701
    /* 16-color block encoding: each 2x2 block is a different color */
702
    CHECK_STREAM_PTR(16);
703
704
    for (y = 0; y < 8; y += 2) {
705
        for (x = 0; x < 8; x += 2) {
706 bf649a9d Mike Melanson
            pix = *s->stream_ptr++;
707
            *(s->pixel_ptr + x) = pix;
708
            *(s->pixel_ptr + x + 1) = pix;
709
            *(s->pixel_ptr + s->stride + x) = pix;
710
            *(s->pixel_ptr + s->stride + x + 1) = pix;
711 186447f8 Mike Melanson
        }
712 bf649a9d Mike Melanson
        s->pixel_ptr += s->stride * 2;
713 186447f8 Mike Melanson
    }
714
715
    /* report success */
716
    return 0;
717
}
718
719 bf649a9d Mike Melanson
static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
720 186447f8 Mike Melanson
{
721
    int x, y;
722
    unsigned char P[4];
723
    unsigned char index = 0;
724
725
    /* 4-color block encoding: each 4x4 block is a different color */
726
    CHECK_STREAM_PTR(4);
727
728
    for (y = 0; y < 4; y++)
729 bf649a9d Mike Melanson
        P[y] = *s->stream_ptr++;
730 186447f8 Mike Melanson
731
    for (y = 0; y < 8; y++) {
732
        if (y < 4)
733
            index = 0;
734
        else
735
            index = 2;
736
737
        for (x = 0; x < 8; x++) {
738
            if (x == 4)
739
                index++;
740 bf649a9d Mike Melanson
            *s->pixel_ptr++ = P[index];
741 186447f8 Mike Melanson
        }
742 bf649a9d Mike Melanson
        s->pixel_ptr += s->line_inc;
743 186447f8 Mike Melanson
    }
744
745
    /* report success */
746
    return 0;
747
}
748
749 bf649a9d Mike Melanson
static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
750 186447f8 Mike Melanson
{
751
    int x, y;
752
    unsigned char pix;
753
754
    /* 1-color encoding: the whole block is 1 solid color */
755
    CHECK_STREAM_PTR(1);
756 bf649a9d Mike Melanson
    pix = *s->stream_ptr++;
757 186447f8 Mike Melanson
758
    for (y = 0; y < 8; y++) {
759
        for (x = 0; x < 8; x++) {
760 bf649a9d Mike Melanson
            *s->pixel_ptr++ = pix;
761 186447f8 Mike Melanson
        }
762 bf649a9d Mike Melanson
        s->pixel_ptr += s->line_inc;
763 186447f8 Mike Melanson
    }
764
765
    /* report success */
766
    return 0;
767
}
768
769 bf649a9d Mike Melanson
static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
770 186447f8 Mike Melanson
{
771
    int x, y;
772
    unsigned char sample0, sample1;
773
774
    /* dithered encoding */
775
    CHECK_STREAM_PTR(2);
776 bf649a9d Mike Melanson
    sample0 = *s->stream_ptr++;
777
    sample1 = *s->stream_ptr++;
778 186447f8 Mike Melanson
779
    for (y = 0; y < 8; y++) {
780
        for (x = 0; x < 8; x += 2) {
781
            if (y & 1) {
782 bf649a9d Mike Melanson
                *s->pixel_ptr++ = sample1;
783
                *s->pixel_ptr++ = sample0;
784 186447f8 Mike Melanson
            } else {
785 bf649a9d Mike Melanson
                *s->pixel_ptr++ = sample0;
786
                *s->pixel_ptr++ = sample1;
787 186447f8 Mike Melanson
            }
788
        }
789 bf649a9d Mike Melanson
        s->pixel_ptr += s->line_inc;
790 186447f8 Mike Melanson
    }
791
792
    /* report success */
793
    return 0;
794
}
795
796 bf649a9d Mike Melanson
static int (*ipvideo_decode_block[16])(IpvideoContext *s);
797 186447f8 Mike Melanson
798
static void ipvideo_decode_opcodes(IpvideoContext *s)
799
{
800
    int x, y;
801
    int index = 0;
802
    unsigned char opcode;
803
    int ret;
804
    int code_counts[16];
805
    static int frame = 0;
806
807
    debug_interplay("------------------ frame %d\n", frame);
808
    frame++;
809
810
    for (x = 0; x < 16; x++)
811
        code_counts[x] = 0;
812
813
    /* this is PAL8, so make the palette available */
814 2a2bbcb0 Mike Melanson
    memcpy(s->current_frame.data[1], s->avctx->palctrl->palette, PALETTE_COUNT * 4);
815 bf649a9d Mike Melanson
816
    s->stride = s->current_frame.linesize[0];
817
    s->stream_ptr = s->buf + 14;  /* data starts 14 bytes in */
818
    s->stream_end = s->buf + s->size;
819
    s->line_inc = s->stride - 8;
820
    s->upper_motion_limit_offset = (s->avctx->height - 8) * s->stride
821
        + s->avctx->width - 8;
822
    s->dsp = s->dsp;
823
824
    for (y = 0; y < (s->stride * s->avctx->height); y += s->stride * 8) {
825
        for (x = y; x < y + s->avctx->width; x += 8) {
826
            /* bottom nibble first, then top nibble (which makes it
827
             * hard to use a GetBitcontext) */
828
            if (index & 1)
829
                opcode = s->decoding_map[index >> 1] >> 4;
830
            else
831
                opcode = s->decoding_map[index >> 1] & 0xF;
832
            index++;
833
834
            debug_interplay("  block @ (%3d, %3d): encoding 0x%X, data ptr @ %p\n",
835
                x - y, y / s->stride, opcode, s->stream_ptr);
836
            code_counts[opcode]++;
837
838
            s->pixel_ptr = s->current_frame.data[0] + x;
839
            ret = ipvideo_decode_block[opcode](s);
840
            if (ret != 0) {
841 9b879566 Michel Bardiaux
                av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
842 bf649a9d Mike Melanson
                    frame, x - y, y / s->stride);
843
                return;
844 186447f8 Mike Melanson
            }
845
        }
846
    }
847 bf649a9d Mike Melanson
    if ((s->stream_ptr != s->stream_end) &&
848
        (s->stream_ptr + 1 != s->stream_end)) {
849 4733abcb Måns Rullgård
        av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode finished with %td bytes left over\n",
850 bf649a9d Mike Melanson
            s->stream_end - s->stream_ptr);
851
    }
852 186447f8 Mike Melanson
}
853
854 3ef8be2b Mike Melanson
static int ipvideo_decode_init(AVCodecContext *avctx)
855
{
856
    IpvideoContext *s = avctx->priv_data;
857
858
    s->avctx = avctx;
859 186447f8 Mike Melanson
860 2a2bbcb0 Mike Melanson
    if (s->avctx->palctrl == NULL) {
861 9b879566 Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, " Interplay video: palette expected.\n");
862 186447f8 Mike Melanson
        return -1;
863
    }
864
865
    avctx->pix_fmt = PIX_FMT_PAL8;
866 3ef8be2b Mike Melanson
    avctx->has_b_frames = 0;
867
    dsputil_init(&s->dsp, avctx);
868
869
    /* decoding map contains 4 bits of information per 8x8 block */
870
    s->decoding_map_size = avctx->width * avctx->height / (8 * 8 * 2);
871 186447f8 Mike Melanson
872
    /* assign block decode functions */
873 d86e8568 Mike Melanson
    ipvideo_decode_block[0x0] = ipvideo_decode_block_opcode_0x0;
874
    ipvideo_decode_block[0x1] = ipvideo_decode_block_opcode_0x1;
875 186447f8 Mike Melanson
    ipvideo_decode_block[0x2] = ipvideo_decode_block_opcode_0x2;
876
    ipvideo_decode_block[0x3] = ipvideo_decode_block_opcode_0x3;
877
    ipvideo_decode_block[0x4] = ipvideo_decode_block_opcode_0x4;
878
    ipvideo_decode_block[0x5] = ipvideo_decode_block_opcode_0x5;
879
    ipvideo_decode_block[0x6] = ipvideo_decode_block_opcode_0x6;
880
    ipvideo_decode_block[0x7] = ipvideo_decode_block_opcode_0x7;
881
    ipvideo_decode_block[0x8] = ipvideo_decode_block_opcode_0x8;
882
    ipvideo_decode_block[0x9] = ipvideo_decode_block_opcode_0x9;
883
    ipvideo_decode_block[0xA] = ipvideo_decode_block_opcode_0xA;
884
    ipvideo_decode_block[0xB] = ipvideo_decode_block_opcode_0xB;
885
    ipvideo_decode_block[0xC] = ipvideo_decode_block_opcode_0xC;
886
    ipvideo_decode_block[0xD] = ipvideo_decode_block_opcode_0xD;
887
    ipvideo_decode_block[0xE] = ipvideo_decode_block_opcode_0xE;
888
    ipvideo_decode_block[0xF] = ipvideo_decode_block_opcode_0xF;
889 3ef8be2b Mike Melanson
890 d86e8568 Mike Melanson
    s->current_frame.data[0] = s->last_frame.data[0] =
891
    s->second_last_frame.data[0] = NULL;
892
893 3ef8be2b Mike Melanson
    return 0;
894
}
895
896
static int ipvideo_decode_frame(AVCodecContext *avctx,
897
                                void *data, int *data_size,
898
                                uint8_t *buf, int buf_size)
899
{
900
    IpvideoContext *s = avctx->priv_data;
901 2a2bbcb0 Mike Melanson
    AVPaletteControl *palette_control = avctx->palctrl;
902 3ef8be2b Mike Melanson
903 18a508c5 Mike Melanson
    /* compressed buffer needs to be large enough to at least hold an entire
904
     * decoding map */
905
    if (buf_size < s->decoding_map_size)
906
        return buf_size;
907
908 186447f8 Mike Melanson
    s->decoding_map = buf;
909
    s->buf = buf + s->decoding_map_size;
910
    s->size = buf_size - s->decoding_map_size;
911 3ef8be2b Mike Melanson
912 d86e8568 Mike Melanson
    s->current_frame.reference = 3;
913 186447f8 Mike Melanson
    if (avctx->get_buffer(avctx, &s->current_frame)) {
914 9b879566 Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "  Interplay Video: get_buffer() failed\n");
915 186447f8 Mike Melanson
        return -1;
916
    }
917 3ef8be2b Mike Melanson
918 186447f8 Mike Melanson
    ipvideo_decode_opcodes(s);
919 3ef8be2b Mike Melanson
920 2a2bbcb0 Mike Melanson
    if (palette_control->palette_changed) {
921
        palette_control->palette_changed = 0;
922
        s->current_frame.palette_has_changed = 1;
923
    }
924
925 d86e8568 Mike Melanson
    *data_size = sizeof(AVFrame);
926
    *(AVFrame*)data = s->current_frame;
927 3ef8be2b Mike Melanson
928 186447f8 Mike Melanson
    /* shuffle frames */
929 d86e8568 Mike Melanson
    if (s->second_last_frame.data[0])
930
        avctx->release_buffer(avctx, &s->second_last_frame);
931
    s->second_last_frame = s->last_frame;
932 186447f8 Mike Melanson
    s->last_frame = s->current_frame;
933 d86e8568 Mike Melanson
    s->current_frame.data[0] = NULL;  /* catch any access attempts */
934 3ef8be2b Mike Melanson
935 186447f8 Mike Melanson
    /* report that the buffer was completely consumed */
936 3ef8be2b Mike Melanson
    return buf_size;
937
}
938
939
static int ipvideo_decode_end(AVCodecContext *avctx)
940
{
941
    IpvideoContext *s = avctx->priv_data;
942
943
    /* release the last frame */
944 d86e8568 Mike Melanson
    if (s->last_frame.data[0])
945
        avctx->release_buffer(avctx, &s->last_frame);
946
    if (s->second_last_frame.data[0])
947
        avctx->release_buffer(avctx, &s->second_last_frame);
948 3ef8be2b Mike Melanson
949
    return 0;
950
}
951
952
AVCodec interplay_video_decoder = {
953
    "interplayvideo",
954
    CODEC_TYPE_VIDEO,
955
    CODEC_ID_INTERPLAY_VIDEO,
956
    sizeof(IpvideoContext),
957
    ipvideo_decode_init,
958
    NULL,
959
    ipvideo_decode_end,
960
    ipvideo_decode_frame,
961
    CODEC_CAP_DR1,
962
};