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