Statistics
| Branch: | Revision:

ffmpeg / libavcodec / interplayvideo.c @ 0fb669ae

History | View | Annotate | Download (19.8 KB)

1
/*
2
 * Interplay MVE Video Decoder
3
 * Copyright (C) 2003 the ffmpeg project
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
/**
23
 * @file libavcodec/interplayvideo.c
24
 * Interplay MVE Video Decoder by Mike Melanson (melanson@pcisys.net)
25
 * For more information about the Interplay MVE format, visit:
26
 *   http://www.pcisys.net/~melanson/codecs/interplay-mve.txt
27
 * This code is written in such a way that the identifiers match up
28
 * with the encoding descriptions in the document.
29
 *
30
 * This decoder presently only supports a PAL8 output colorspace.
31
 *
32
 * An Interplay video frame consists of 2 parts: The decoding map and
33
 * the video data. A demuxer must load these 2 parts together in a single
34
 * buffer before sending it through the stream to this decoder.
35
 */
36

    
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40

    
41
#include "avcodec.h"
42
#include "bytestream.h"
43
#include "dsputil.h"
44
#define ALT_BITSTREAM_READER_LE
45
#include "get_bits.h"
46

    
47
#define PALETTE_COUNT 256
48

    
49
/* debugging support */
50
#define DEBUG_INTERPLAY 0
51
#if DEBUG_INTERPLAY
52
#define debug_interplay(x,...) av_log(NULL, AV_LOG_DEBUG, x, __VA_ARGS__)
53
#else
54
static inline void debug_interplay(const char *format, ...) { }
55
#endif
56

    
57
typedef struct IpvideoContext {
58

    
59
    AVCodecContext *avctx;
60
    DSPContext dsp;
61
    AVFrame second_last_frame;
62
    AVFrame last_frame;
63
    AVFrame current_frame;
64
    const unsigned char *decoding_map;
65
    int decoding_map_size;
66

    
67
    const unsigned char *buf;
68
    int size;
69

    
70
    const unsigned char *stream_ptr;
71
    const unsigned char *stream_end;
72
    unsigned char *pixel_ptr;
73
    int line_inc;
74
    int stride;
75
    int upper_motion_limit_offset;
76

    
77
} IpvideoContext;
78

    
79
#define CHECK_STREAM_PTR(n) \
80
    if (s->stream_end - s->stream_ptr < n) { \
81
        av_log(s->avctx, AV_LOG_ERROR, "Interplay video warning: stream_ptr out of bounds (%p >= %p)\n", \
82
               s->stream_ptr + n, s->stream_end); \
83
        return -1; \
84
    }
85

    
86
static int copy_from(IpvideoContext *s, AVFrame *src, int delta_x, int delta_y)
87
{
88
    int current_offset = s->pixel_ptr - s->current_frame.data[0];
89
    int motion_offset = current_offset + delta_y * s->stride + delta_x;
90
    if (motion_offset < 0) {
91
        av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset < 0 (%d)\n", motion_offset);
92
        return -1;
93
    } else if (motion_offset > s->upper_motion_limit_offset) {
94
        av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset above limit (%d >= %d)\n",
95
            motion_offset, s->upper_motion_limit_offset);
96
        return -1;
97
    }
98
    s->dsp.put_pixels_tab[1][0](s->pixel_ptr, src->data[0] + motion_offset, s->stride, 8);
99
    return 0;
100
}
101

    
102
static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s)
103
{
104
    return copy_from(s, &s->last_frame, 0, 0);
105
}
106

    
107
static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s)
108
{
109
    return copy_from(s, &s->second_last_frame, 0, 0);
110
}
111

    
112
static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s)
113
{
114
    unsigned char B;
115
    int x, y;
116

    
117
    /* copy block from 2 frames ago using a motion vector; need 1 more byte */
118
    CHECK_STREAM_PTR(1);
119
    B = *s->stream_ptr++;
120

    
121
    if (B < 56) {
122
        x = 8 + (B % 7);
123
        y = B / 7;
124
    } else {
125
        x = -14 + ((B - 56) % 29);
126
        y =   8 + ((B - 56) / 29);
127
    }
128

    
129
    debug_interplay ("    motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
130
    return copy_from(s, &s->second_last_frame, x, y);
131
}
132

    
133
static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s)
134
{
135
    unsigned char B;
136
    int x, y;
137

    
138
    /* copy 8x8 block from current frame from an up/left block */
139

    
140
    /* need 1 more byte for motion */
141
    CHECK_STREAM_PTR(1);
142
    B = *s->stream_ptr++;
143

    
144
    if (B < 56) {
145
        x = -(8 + (B % 7));
146
        y = -(B / 7);
147
    } else {
148
        x = -(-14 + ((B - 56) % 29));
149
        y = -(  8 + ((B - 56) / 29));
150
    }
151

    
152
    debug_interplay ("    motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
153
    return copy_from(s, &s->current_frame, x, y);
154
}
155

    
156
static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s)
157
{
158
    int x, y;
159
    unsigned char B, BL, BH;
160

    
161
    /* copy a block from the previous frame; need 1 more byte */
162
    CHECK_STREAM_PTR(1);
163

    
164
    B = *s->stream_ptr++;
165
    BL = B & 0x0F;
166
    BH = (B >> 4) & 0x0F;
167
    x = -8 + BL;
168
    y = -8 + BH;
169

    
170
    debug_interplay ("    motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
171
    return copy_from(s, &s->last_frame, x, y);
172
}
173

    
174
static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s)
175
{
176
    signed char x, y;
177

    
178
    /* copy a block from the previous frame using an expanded range;
179
     * need 2 more bytes */
180
    CHECK_STREAM_PTR(2);
181

    
182
    x = *s->stream_ptr++;
183
    y = *s->stream_ptr++;
184

    
185
    debug_interplay ("    motion bytes = %d, %d\n", x, y);
186
    return copy_from(s, &s->last_frame, x, y);
187
}
188

    
189
static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s)
190
{
191
    /* mystery opcode? skip multiple blocks? */
192
    av_log(s->avctx, AV_LOG_ERROR, "  Interplay video: Help! Mystery opcode 0x6 seen\n");
193

    
194
    /* report success */
195
    return 0;
196
}
197

    
198
static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s)
199
{
200
    int x, y;
201
    unsigned char P[2];
202
    unsigned int flags;
203

    
204
    /* 2-color encoding */
205
    CHECK_STREAM_PTR(2);
206

    
207
    P[0] = *s->stream_ptr++;
208
    P[1] = *s->stream_ptr++;
209

    
210
    if (P[0] <= P[1]) {
211

    
212
        /* need 8 more bytes from the stream */
213
        CHECK_STREAM_PTR(8);
214

    
215
        for (y = 0; y < 8; y++) {
216
            flags = *s->stream_ptr++ | 0x100;
217
            for (; flags != 1; flags >>= 1)
218
                *s->pixel_ptr++ = P[flags & 1];
219
            s->pixel_ptr += s->line_inc;
220
        }
221

    
222
    } else {
223

    
224
        /* need 2 more bytes from the stream */
225
        CHECK_STREAM_PTR(2);
226

    
227
        flags = bytestream_get_le16(&s->stream_ptr);
228
        for (y = 0; y < 8; y += 2) {
229
            for (x = 0; x < 8; x += 2, flags >>= 1) {
230
                s->pixel_ptr[x                ] =
231
                s->pixel_ptr[x + 1            ] =
232
                s->pixel_ptr[x +     s->stride] =
233
                s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
234
            }
235
            s->pixel_ptr += s->stride * 2;
236
        }
237
    }
238

    
239
    /* report success */
240
    return 0;
241
}
242

    
243
static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s)
244
{
245
    int x, y;
246
    unsigned char P[2];
247
    unsigned int flags = 0;
248

    
249
    /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
250
     * either top and bottom or left and right halves */
251
    CHECK_STREAM_PTR(2);
252

    
253
    P[0] = *s->stream_ptr++;
254
    P[1] = *s->stream_ptr++;
255

    
256
    if (P[0] <= P[1]) {
257

    
258
        CHECK_STREAM_PTR(14);
259
        s->stream_ptr -= 2;
260

    
261
        for (y = 0; y < 16; y++) {
262
            // new values for each 4x4 block
263
            if (!(y & 3)) {
264
                P[0] = *s->stream_ptr++; P[1] = *s->stream_ptr++;
265
                flags = bytestream_get_le16(&s->stream_ptr);
266
            }
267

    
268
            for (x = 0; x < 4; x++, flags >>= 1)
269
                *s->pixel_ptr++ = P[flags & 1];
270
            s->pixel_ptr += s->stride - 4;
271
            // switch to right half
272
            if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
273
        }
274

    
275
    } else {
276

    
277
        /* need 10 more bytes */
278
        CHECK_STREAM_PTR(10);
279

    
280
        if (s->stream_ptr[4] <= s->stream_ptr[5]) {
281

    
282
            flags = bytestream_get_le32(&s->stream_ptr);
283

    
284
            /* vertical split; left & right halves are 2-color encoded */
285

    
286
            for (y = 0; y < 16; y++) {
287
                for (x = 0; x < 4; x++, flags >>= 1)
288
                    *s->pixel_ptr++ = P[flags & 1];
289
                s->pixel_ptr += s->stride - 4;
290
                // switch to right half
291
                if (y == 7) {
292
                    s->pixel_ptr -= 8 * s->stride - 4;
293
                    P[0] = *s->stream_ptr++; P[1] = *s->stream_ptr++;
294
                    flags = bytestream_get_le32(&s->stream_ptr);
295
                }
296
            }
297

    
298
        } else {
299

    
300
            /* horizontal split; top & bottom halves are 2-color encoded */
301

    
302
            for (y = 0; y < 8; y++) {
303
                if (y == 4) {
304
                    P[0] = *s->stream_ptr++;
305
                    P[1] = *s->stream_ptr++;
306
                }
307
                flags = *s->stream_ptr++ | 0x100;
308

    
309
                for (; flags != 1; flags >>= 1)
310
                    *s->pixel_ptr++ = P[flags & 1];
311
                s->pixel_ptr += s->line_inc;
312
            }
313
        }
314
    }
315

    
316
    /* report success */
317
    return 0;
318
}
319

    
320
static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
321
{
322
    int x, y;
323
    unsigned char P[4];
324

    
325
    /* 4-color encoding */
326
    CHECK_STREAM_PTR(4);
327

    
328
    memcpy(P, s->stream_ptr, 4);
329
    s->stream_ptr += 4;
330

    
331
    if (P[0] <= P[1]) {
332
        if (P[2] <= P[3]) {
333

    
334
            /* 1 of 4 colors for each pixel, need 16 more bytes */
335
            CHECK_STREAM_PTR(16);
336

    
337
            for (y = 0; y < 8; y++) {
338
                /* get the next set of 8 2-bit flags */
339
                int flags = bytestream_get_le16(&s->stream_ptr);
340
                for (x = 0; x < 8; x++, flags >>= 2)
341
                    *s->pixel_ptr++ = P[flags & 0x03];
342
                s->pixel_ptr += s->line_inc;
343
            }
344

    
345
        } else {
346
            uint32_t flags;
347

    
348
            /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
349
            CHECK_STREAM_PTR(4);
350

    
351
            flags = bytestream_get_le32(&s->stream_ptr);
352

    
353
            for (y = 0; y < 8; y += 2) {
354
                for (x = 0; x < 8; x += 2, flags >>= 2) {
355
                    s->pixel_ptr[x                ] =
356
                    s->pixel_ptr[x + 1            ] =
357
                    s->pixel_ptr[x +     s->stride] =
358
                    s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
359
                }
360
                s->pixel_ptr += s->stride * 2;
361
            }
362

    
363
        }
364
    } else {
365
        uint64_t flags;
366

    
367
        /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
368
        CHECK_STREAM_PTR(8);
369

    
370
        flags = bytestream_get_le64(&s->stream_ptr);
371
        if (P[2] <= P[3]) {
372
            for (y = 0; y < 8; y++) {
373
                for (x = 0; x < 8; x += 2, flags >>= 2) {
374
                    s->pixel_ptr[x    ] =
375
                    s->pixel_ptr[x + 1] = P[flags & 0x03];
376
                }
377
                s->pixel_ptr += s->stride;
378
            }
379
        } else {
380
            for (y = 0; y < 8; y += 2) {
381
                for (x = 0; x < 8; x++, flags >>= 2) {
382
                    s->pixel_ptr[x            ] =
383
                    s->pixel_ptr[x + s->stride] = P[flags & 0x03];
384
                }
385
                s->pixel_ptr += s->stride * 2;
386
            }
387
        }
388
    }
389

    
390
    /* report success */
391
    return 0;
392
}
393

    
394
static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
395
{
396
    int x, y;
397
    unsigned char P[4];
398
    int flags = 0;
399

    
400
    /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
401
     * either top and bottom or left and right halves */
402
    CHECK_STREAM_PTR(24);
403

    
404
    if (s->stream_ptr[0] <= s->stream_ptr[1]) {
405

    
406
        /* 4-color encoding for each quadrant; need 32 bytes */
407
        CHECK_STREAM_PTR(32);
408

    
409
        for (y = 0; y < 16; y++) {
410
            // new values for each 4x4 block
411
            if (!(y & 3)) {
412
                memcpy(P, s->stream_ptr, 4);
413
                s->stream_ptr += 4;
414
                flags = bytestream_get_le32(&s->stream_ptr);
415
            }
416

    
417
            for (x = 0; x < 4; x++, flags >>= 2)
418
                *s->pixel_ptr++ = P[flags & 0x03];
419

    
420
            s->pixel_ptr += s->stride - 4;
421
            // switch to right half
422
            if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
423
        }
424

    
425
    } else {
426
        // vertical split?
427
        int vert = s->stream_ptr[12] <= s->stream_ptr[13];
428
        uint64_t flags = 0;
429

    
430
        /* 4-color encoding for either left and right or top and bottom
431
         * halves */
432

    
433
        for (y = 0; y < 16; y++) {
434
            // load values for each half
435
            if (!(y & 7)) {
436
                memcpy(P, s->stream_ptr, 4);
437
                s->stream_ptr += 4;
438
                flags = bytestream_get_le64(&s->stream_ptr);
439
            }
440

    
441
            for (x = 0; x < 4; x++, flags >>= 2)
442
                *s->pixel_ptr++ = P[flags & 0x03];
443

    
444
            if (vert) {
445
                s->pixel_ptr += s->stride - 4;
446
                // switch to right half
447
                if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
448
            } else if (y & 1) s->pixel_ptr += s->line_inc;
449
        }
450
    }
451

    
452
    /* report success */
453
    return 0;
454
}
455

    
456
static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
457
{
458
    int y;
459

    
460
    /* 64-color encoding (each pixel in block is a different color) */
461
    CHECK_STREAM_PTR(64);
462

    
463
    for (y = 0; y < 8; y++) {
464
        memcpy(s->pixel_ptr, s->stream_ptr, 8);
465
        s->stream_ptr += 8;
466
        s->pixel_ptr  += s->stride;
467
    }
468

    
469
    /* report success */
470
    return 0;
471
}
472

    
473
static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
474
{
475
    int x, y;
476

    
477
    /* 16-color block encoding: each 2x2 block is a different color */
478
    CHECK_STREAM_PTR(16);
479

    
480
    for (y = 0; y < 8; y += 2) {
481
        for (x = 0; x < 8; x += 2) {
482
            s->pixel_ptr[x                ] =
483
            s->pixel_ptr[x + 1            ] =
484
            s->pixel_ptr[x +     s->stride] =
485
            s->pixel_ptr[x + 1 + s->stride] = *s->stream_ptr++;
486
        }
487
        s->pixel_ptr += s->stride * 2;
488
    }
489

    
490
    /* report success */
491
    return 0;
492
}
493

    
494
static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
495
{
496
    int y;
497
    unsigned char P[2];
498

    
499
    /* 4-color block encoding: each 4x4 block is a different color */
500
    CHECK_STREAM_PTR(4);
501

    
502
    for (y = 0; y < 8; y++) {
503
        if (!(y & 3)) {
504
            P[0] = *s->stream_ptr++;
505
            P[1] = *s->stream_ptr++;
506
        }
507
        memset(s->pixel_ptr,     P[0], 4);
508
        memset(s->pixel_ptr + 4, P[1], 4);
509
        s->pixel_ptr += s->stride;
510
    }
511

    
512
    /* report success */
513
    return 0;
514
}
515

    
516
static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
517
{
518
    int y;
519
    unsigned char pix;
520

    
521
    /* 1-color encoding: the whole block is 1 solid color */
522
    CHECK_STREAM_PTR(1);
523
    pix = *s->stream_ptr++;
524

    
525
    for (y = 0; y < 8; y++) {
526
        memset(s->pixel_ptr, pix, 8);
527
        s->pixel_ptr += s->stride;
528
    }
529

    
530
    /* report success */
531
    return 0;
532
}
533

    
534
static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
535
{
536
    int x, y;
537
    unsigned char sample[2];
538

    
539
    /* dithered encoding */
540
    CHECK_STREAM_PTR(2);
541
    sample[0] = *s->stream_ptr++;
542
    sample[1] = *s->stream_ptr++;
543

    
544
    for (y = 0; y < 8; y++) {
545
        for (x = 0; x < 8; x += 2) {
546
            *s->pixel_ptr++ = sample[  y & 1 ];
547
            *s->pixel_ptr++ = sample[!(y & 1)];
548
        }
549
        s->pixel_ptr += s->line_inc;
550
    }
551

    
552
    /* report success */
553
    return 0;
554
}
555

    
556
static int (* const ipvideo_decode_block[])(IpvideoContext *s) = {
557
    ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
558
    ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
559
    ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
560
    ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
561
    ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
562
    ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
563
    ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
564
    ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
565
};
566

    
567
static void ipvideo_decode_opcodes(IpvideoContext *s)
568
{
569
    int x, y;
570
    unsigned char opcode;
571
    int ret;
572
    static int frame = 0;
573
    GetBitContext gb;
574

    
575
    debug_interplay("------------------ frame %d\n", frame);
576
    frame++;
577

    
578
    /* this is PAL8, so make the palette available */
579
    memcpy(s->current_frame.data[1], s->avctx->palctrl->palette, PALETTE_COUNT * 4);
580

    
581
    s->stride = s->current_frame.linesize[0];
582
    s->stream_ptr = s->buf + 14;  /* data starts 14 bytes in */
583
    s->stream_end = s->buf + s->size;
584
    s->line_inc = s->stride - 8;
585
    s->upper_motion_limit_offset = (s->avctx->height - 8) * s->stride
586
        + s->avctx->width - 8;
587

    
588
    init_get_bits(&gb, s->decoding_map, s->decoding_map_size * 8);
589
    for (y = 0; y < (s->stride * s->avctx->height); y += s->stride * 8) {
590
        for (x = y; x < y + s->avctx->width; x += 8) {
591
            opcode = get_bits(&gb, 4);
592

    
593
            debug_interplay("  block @ (%3d, %3d): encoding 0x%X, data ptr @ %p\n",
594
                            x - y, y / s->stride, opcode, s->stream_ptr);
595

    
596
            s->pixel_ptr = s->current_frame.data[0] + x;
597
            ret = ipvideo_decode_block[opcode](s);
598
            if (ret != 0) {
599
                av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
600
                       frame, x - y, y / s->stride);
601
                return;
602
            }
603
        }
604
    }
605
    if (s->stream_end - s->stream_ptr > 1) {
606
        av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode finished with %td bytes left over\n",
607
               s->stream_end - s->stream_ptr);
608
    }
609
}
610

    
611
static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
612
{
613
    IpvideoContext *s = avctx->priv_data;
614

    
615
    s->avctx = avctx;
616

    
617
    if (s->avctx->palctrl == NULL) {
618
        av_log(avctx, AV_LOG_ERROR, " Interplay video: palette expected.\n");
619
        return -1;
620
    }
621

    
622
    avctx->pix_fmt = PIX_FMT_PAL8;
623
    dsputil_init(&s->dsp, avctx);
624

    
625
    /* decoding map contains 4 bits of information per 8x8 block */
626
    s->decoding_map_size = avctx->width * avctx->height / (8 * 8 * 2);
627

    
628
    s->current_frame.data[0] = s->last_frame.data[0] =
629
    s->second_last_frame.data[0] = NULL;
630

    
631
    return 0;
632
}
633

    
634
static int ipvideo_decode_frame(AVCodecContext *avctx,
635
                                void *data, int *data_size,
636
                                AVPacket *avpkt)
637
{
638
    const uint8_t *buf = avpkt->data;
639
    int buf_size = avpkt->size;
640
    IpvideoContext *s = avctx->priv_data;
641
    AVPaletteControl *palette_control = avctx->palctrl;
642

    
643
    /* compressed buffer needs to be large enough to at least hold an entire
644
     * decoding map */
645
    if (buf_size < s->decoding_map_size)
646
        return buf_size;
647

    
648
    s->decoding_map = buf;
649
    s->buf = buf + s->decoding_map_size;
650
    s->size = buf_size - s->decoding_map_size;
651

    
652
    s->current_frame.reference = 3;
653
    if (avctx->get_buffer(avctx, &s->current_frame)) {
654
        av_log(avctx, AV_LOG_ERROR, "  Interplay Video: get_buffer() failed\n");
655
        return -1;
656
    }
657

    
658
    ipvideo_decode_opcodes(s);
659

    
660
    if (palette_control->palette_changed) {
661
        palette_control->palette_changed = 0;
662
        s->current_frame.palette_has_changed = 1;
663
    }
664

    
665
    *data_size = sizeof(AVFrame);
666
    *(AVFrame*)data = s->current_frame;
667

    
668
    /* shuffle frames */
669
    if (s->second_last_frame.data[0])
670
        avctx->release_buffer(avctx, &s->second_last_frame);
671
    s->second_last_frame = s->last_frame;
672
    s->last_frame = s->current_frame;
673
    s->current_frame.data[0] = NULL;  /* catch any access attempts */
674

    
675
    /* report that the buffer was completely consumed */
676
    return buf_size;
677
}
678

    
679
static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
680
{
681
    IpvideoContext *s = avctx->priv_data;
682

    
683
    /* release the last frame */
684
    if (s->last_frame.data[0])
685
        avctx->release_buffer(avctx, &s->last_frame);
686
    if (s->second_last_frame.data[0])
687
        avctx->release_buffer(avctx, &s->second_last_frame);
688

    
689
    return 0;
690
}
691

    
692
AVCodec interplay_video_decoder = {
693
    "interplayvideo",
694
    CODEC_TYPE_VIDEO,
695
    CODEC_ID_INTERPLAY_VIDEO,
696
    sizeof(IpvideoContext),
697
    ipvideo_decode_init,
698
    NULL,
699
    ipvideo_decode_end,
700
    ipvideo_decode_frame,
701
    CODEC_CAP_DR1,
702
    .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
703
};