]> git.sesse.net Git - ffmpeg/blob - libavcodec/interplayvideo.c
imdct15: rename to mdct15 and add a forward transform
[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
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 "libavutil/intreadwrite.h"
42
43 #define BITSTREAM_READER_LE
44 #include "avcodec.h"
45 #include "bytestream.h"
46 #include "get_bits.h"
47 #include "hpeldsp.h"
48 #include "internal.h"
49
50 #define PALETTE_COUNT 256
51
52 typedef struct IpvideoContext {
53
54     AVCodecContext *avctx;
55     HpelDSPContext hdsp;
56     AVFrame *second_last_frame;
57     AVFrame *last_frame;
58     const unsigned char *decoding_map;
59     int decoding_map_size;
60
61     int is_16bpp;
62     GetByteContext stream_ptr, mv_ptr;
63     unsigned char *pixel_ptr;
64     int line_inc;
65     int stride;
66     int upper_motion_limit_offset;
67
68     uint32_t pal[256];
69 } IpvideoContext;
70
71 static int copy_from(IpvideoContext *s, AVFrame *src, AVFrame *dst, int delta_x, int delta_y)
72 {
73     int current_offset = s->pixel_ptr - dst->data[0];
74     int motion_offset = current_offset + delta_y * dst->linesize[0]
75                        + delta_x * (1 + s->is_16bpp);
76     if (motion_offset < 0) {
77         av_log(s->avctx, AV_LOG_ERROR, "motion offset < 0 (%d)\n", motion_offset);
78         return AVERROR_INVALIDDATA;
79     } else if (motion_offset > s->upper_motion_limit_offset) {
80         av_log(s->avctx, AV_LOG_ERROR, "motion offset above limit (%d >= %d)\n",
81             motion_offset, s->upper_motion_limit_offset);
82         return AVERROR_INVALIDDATA;
83     }
84     if (!src->data[0]) {
85         av_log(s->avctx, AV_LOG_ERROR, "Invalid decode type, corrupted header?\n");
86         return AVERROR(EINVAL);
87     }
88     s->hdsp.put_pixels_tab[!s->is_16bpp][0](s->pixel_ptr, src->data[0] + motion_offset,
89                                             dst->linesize[0], 8);
90     return 0;
91 }
92
93 static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s, AVFrame *frame)
94 {
95     return copy_from(s, s->last_frame, frame, 0, 0);
96 }
97
98 static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s, AVFrame *frame)
99 {
100     return copy_from(s, s->second_last_frame, frame, 0, 0);
101 }
102
103 static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s, AVFrame *frame)
104 {
105     unsigned char B;
106     int x, y;
107
108     /* copy block from 2 frames ago using a motion vector; need 1 more byte */
109     if (!s->is_16bpp) {
110         B = bytestream2_get_byte(&s->stream_ptr);
111     } else {
112         B = bytestream2_get_byte(&s->mv_ptr);
113     }
114
115     if (B < 56) {
116         x = 8 + (B % 7);
117         y = B / 7;
118     } else {
119         x = -14 + ((B - 56) % 29);
120         y =   8 + ((B - 56) / 29);
121     }
122
123     ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
124     return copy_from(s, s->second_last_frame, frame, x, y);
125 }
126
127 static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s, AVFrame *frame)
128 {
129     unsigned char B;
130     int x, y;
131
132     /* copy 8x8 block from current frame from an up/left block */
133
134     /* need 1 more byte for motion */
135     if (!s->is_16bpp) {
136         B = bytestream2_get_byte(&s->stream_ptr);
137     } else {
138         B = bytestream2_get_byte(&s->mv_ptr);
139     }
140
141     if (B < 56) {
142         x = -(8 + (B % 7));
143         y = -(B / 7);
144     } else {
145         x = -(-14 + ((B - 56) % 29));
146         y = -(  8 + ((B - 56) / 29));
147     }
148
149     ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
150     return copy_from(s, frame, frame, x, y);
151 }
152
153 static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s, AVFrame *frame)
154 {
155     int x, y;
156     unsigned char B, BL, BH;
157
158     /* copy a block from the previous frame; need 1 more byte */
159     if (!s->is_16bpp) {
160         B = bytestream2_get_byte(&s->stream_ptr);
161     } else {
162         B = bytestream2_get_byte(&s->mv_ptr);
163     }
164
165     BL = B & 0x0F;
166     BH = (B >> 4) & 0x0F;
167     x = -8 + BL;
168     y = -8 + BH;
169
170     ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
171     return copy_from(s, s->last_frame, frame, x, y);
172 }
173
174 static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s, AVFrame *frame)
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     x = bytestream2_get_byte(&s->stream_ptr);
181     y = bytestream2_get_byte(&s->stream_ptr);
182
183     ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
184     return copy_from(s, s->last_frame, frame, x, y);
185 }
186
187 static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s, AVFrame *frame)
188 {
189     /* mystery opcode? skip multiple blocks? */
190     av_log(s->avctx, AV_LOG_ERROR, "Help! Mystery opcode 0x6 seen\n");
191
192     /* report success */
193     return 0;
194 }
195
196 static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s, AVFrame *frame)
197 {
198     int x, y;
199     unsigned char P[2];
200     unsigned int flags;
201
202     if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
203         av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x7\n");
204         return AVERROR_INVALIDDATA;
205     }
206
207     /* 2-color encoding */
208     P[0] = bytestream2_get_byte(&s->stream_ptr);
209     P[1] = bytestream2_get_byte(&s->stream_ptr);
210
211     if (P[0] <= P[1]) {
212
213         /* need 8 more bytes from the stream */
214         for (y = 0; y < 8; y++) {
215             flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
216             for (; flags != 1; flags >>= 1)
217                 *s->pixel_ptr++ = P[flags & 1];
218             s->pixel_ptr += s->line_inc;
219         }
220
221     } else {
222
223         /* need 2 more bytes from the stream */
224         flags = bytestream2_get_le16(&s->stream_ptr);
225         for (y = 0; y < 8; y += 2) {
226             for (x = 0; x < 8; x += 2, flags >>= 1) {
227                 s->pixel_ptr[x                ] =
228                 s->pixel_ptr[x + 1            ] =
229                 s->pixel_ptr[x +     s->stride] =
230                 s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
231             }
232             s->pixel_ptr += s->stride * 2;
233         }
234     }
235
236     /* report success */
237     return 0;
238 }
239
240 static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s, AVFrame *frame)
241 {
242     int x, y;
243     unsigned char P[4];
244     unsigned int flags = 0;
245
246     if (bytestream2_get_bytes_left(&s->stream_ptr) < 12) {
247         av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x8\n");
248         return AVERROR_INVALIDDATA;
249     }
250
251     /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
252      * either top and bottom or left and right halves */
253     P[0] = bytestream2_get_byte(&s->stream_ptr);
254     P[1] = bytestream2_get_byte(&s->stream_ptr);
255
256     if (P[0] <= P[1]) {
257         for (y = 0; y < 16; y++) {
258             // new values for each 4x4 block
259             if (!(y & 3)) {
260                 if (y) {
261                     P[0]  = bytestream2_get_byte(&s->stream_ptr);
262                     P[1]  = bytestream2_get_byte(&s->stream_ptr);
263                 }
264                 flags = bytestream2_get_le16(&s->stream_ptr);
265             }
266
267             for (x = 0; x < 4; x++, flags >>= 1)
268                 *s->pixel_ptr++ = P[flags & 1];
269             s->pixel_ptr += s->stride - 4;
270             // switch to right half
271             if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
272         }
273
274     } else {
275         flags = bytestream2_get_le32(&s->stream_ptr);
276         P[2] = bytestream2_get_byte(&s->stream_ptr);
277         P[3] = bytestream2_get_byte(&s->stream_ptr);
278
279         if (P[2] <= P[3]) {
280
281             /* vertical split; left & right halves are 2-color encoded */
282
283             for (y = 0; y < 16; y++) {
284                 for (x = 0; x < 4; x++, flags >>= 1)
285                     *s->pixel_ptr++ = P[flags & 1];
286                 s->pixel_ptr += s->stride - 4;
287                 // switch to right half
288                 if (y == 7) {
289                     s->pixel_ptr -= 8 * s->stride - 4;
290                     P[0]  = P[2];
291                     P[1]  = P[3];
292                     flags = bytestream2_get_le32(&s->stream_ptr);
293                 }
294             }
295
296         } else {
297
298             /* horizontal split; top & bottom halves are 2-color encoded */
299
300             for (y = 0; y < 8; y++) {
301                 if (y == 4) {
302                     P[0]  = P[2];
303                     P[1]  = P[3];
304                     flags = bytestream2_get_le32(&s->stream_ptr);
305                 }
306
307                 for (x = 0; x < 8; x++, flags >>= 1)
308                     *s->pixel_ptr++ = P[flags & 1];
309                 s->pixel_ptr += s->line_inc;
310             }
311         }
312     }
313
314     /* report success */
315     return 0;
316 }
317
318 static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s, AVFrame *frame)
319 {
320     int x, y;
321     unsigned char P[4];
322
323     if (bytestream2_get_bytes_left(&s->stream_ptr) < 8) {
324         av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x9\n");
325         return AVERROR_INVALIDDATA;
326     }
327
328     /* 4-color encoding */
329     bytestream2_get_buffer(&s->stream_ptr, P, 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             for (y = 0; y < 8; y++) {
336                 /* get the next set of 8 2-bit flags */
337                 int flags = bytestream2_get_le16(&s->stream_ptr);
338                 for (x = 0; x < 8; x++, flags >>= 2)
339                     *s->pixel_ptr++ = P[flags & 0x03];
340                 s->pixel_ptr += s->line_inc;
341             }
342
343         } else {
344             uint32_t flags;
345
346             /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
347             flags = bytestream2_get_le32(&s->stream_ptr);
348
349             for (y = 0; y < 8; y += 2) {
350                 for (x = 0; x < 8; x += 2, flags >>= 2) {
351                     s->pixel_ptr[x                ] =
352                     s->pixel_ptr[x + 1            ] =
353                     s->pixel_ptr[x +     s->stride] =
354                     s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
355                 }
356                 s->pixel_ptr += s->stride * 2;
357             }
358
359         }
360     } else {
361         uint64_t flags;
362
363         /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
364         flags = bytestream2_get_le64(&s->stream_ptr);
365         if (P[2] <= P[3]) {
366             for (y = 0; y < 8; y++) {
367                 for (x = 0; x < 8; x += 2, flags >>= 2) {
368                     s->pixel_ptr[x    ] =
369                     s->pixel_ptr[x + 1] = P[flags & 0x03];
370                 }
371                 s->pixel_ptr += s->stride;
372             }
373         } else {
374             for (y = 0; y < 8; y += 2) {
375                 for (x = 0; x < 8; x++, flags >>= 2) {
376                     s->pixel_ptr[x            ] =
377                     s->pixel_ptr[x + s->stride] = P[flags & 0x03];
378                 }
379                 s->pixel_ptr += s->stride * 2;
380             }
381         }
382     }
383
384     /* report success */
385     return 0;
386 }
387
388 static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s, AVFrame *frame)
389 {
390     int x, y;
391     unsigned char P[8];
392     int flags = 0;
393
394     if (bytestream2_get_bytes_left(&s->stream_ptr) < 16) {
395         av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xA\n");
396         return AVERROR_INVALIDDATA;
397     }
398
399     bytestream2_get_buffer(&s->stream_ptr, P, 4);
400
401     /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
402      * either top and bottom or left and right halves */
403     if (P[0] <= P[1]) {
404
405         /* 4-color encoding for each quadrant; need 32 bytes */
406         for (y = 0; y < 16; y++) {
407             // new values for each 4x4 block
408             if (!(y & 3)) {
409                 if (y) bytestream2_get_buffer(&s->stream_ptr, P, 4);
410                 flags = bytestream2_get_le32(&s->stream_ptr);
411             }
412
413             for (x = 0; x < 4; x++, flags >>= 2)
414                 *s->pixel_ptr++ = P[flags & 0x03];
415
416             s->pixel_ptr += s->stride - 4;
417             // switch to right half
418             if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
419         }
420
421     } else {
422         // vertical split?
423         int vert;
424         uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
425
426         bytestream2_get_buffer(&s->stream_ptr, P + 4, 4);
427         vert = P[4] <= P[5];
428
429         /* 4-color encoding for either left and right or top and bottom
430          * halves */
431
432         for (y = 0; y < 16; y++) {
433             for (x = 0; x < 4; x++, flags >>= 2)
434                 *s->pixel_ptr++ = P[flags & 0x03];
435
436             if (vert) {
437                 s->pixel_ptr += s->stride - 4;
438                 // switch to right half
439                 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
440             } else if (y & 1) s->pixel_ptr += s->line_inc;
441
442             // load values for second half
443             if (y == 7) {
444                 memcpy(P, P + 4, 4);
445                 flags = bytestream2_get_le64(&s->stream_ptr);
446             }
447         }
448     }
449
450     /* report success */
451     return 0;
452 }
453
454 static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s, AVFrame *frame)
455 {
456     int y;
457
458     /* 64-color encoding (each pixel in block is a different color) */
459     for (y = 0; y < 8; y++) {
460         bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
461         s->pixel_ptr  += s->stride;
462     }
463
464     /* report success */
465     return 0;
466 }
467
468 static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s, AVFrame *frame)
469 {
470     int x, y;
471
472     /* 16-color block encoding: each 2x2 block is a different color */
473     for (y = 0; y < 8; y += 2) {
474         for (x = 0; x < 8; x += 2) {
475             s->pixel_ptr[x                ] =
476             s->pixel_ptr[x + 1            ] =
477             s->pixel_ptr[x +     s->stride] =
478             s->pixel_ptr[x + 1 + s->stride] = bytestream2_get_byte(&s->stream_ptr);
479         }
480         s->pixel_ptr += s->stride * 2;
481     }
482
483     /* report success */
484     return 0;
485 }
486
487 static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s, AVFrame *frame)
488 {
489     int y;
490     unsigned char P[2];
491
492     if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
493         av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xD\n");
494         return AVERROR_INVALIDDATA;
495     }
496
497     /* 4-color block encoding: each 4x4 block is a different color */
498     for (y = 0; y < 8; y++) {
499         if (!(y & 3)) {
500             P[0] = bytestream2_get_byte(&s->stream_ptr);
501             P[1] = bytestream2_get_byte(&s->stream_ptr);
502         }
503         memset(s->pixel_ptr,     P[0], 4);
504         memset(s->pixel_ptr + 4, P[1], 4);
505         s->pixel_ptr += s->stride;
506     }
507
508     /* report success */
509     return 0;
510 }
511
512 static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s, AVFrame *frame)
513 {
514     int y;
515     unsigned char pix;
516
517     /* 1-color encoding: the whole block is 1 solid color */
518     pix = bytestream2_get_byte(&s->stream_ptr);
519
520     for (y = 0; y < 8; y++) {
521         memset(s->pixel_ptr, pix, 8);
522         s->pixel_ptr += s->stride;
523     }
524
525     /* report success */
526     return 0;
527 }
528
529 static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s, AVFrame *frame)
530 {
531     int x, y;
532     unsigned char sample[2];
533
534     /* dithered encoding */
535     sample[0] = bytestream2_get_byte(&s->stream_ptr);
536     sample[1] = bytestream2_get_byte(&s->stream_ptr);
537
538     for (y = 0; y < 8; y++) {
539         for (x = 0; x < 8; x += 2) {
540             *s->pixel_ptr++ = sample[  y & 1 ];
541             *s->pixel_ptr++ = sample[!(y & 1)];
542         }
543         s->pixel_ptr += s->line_inc;
544     }
545
546     /* report success */
547     return 0;
548 }
549
550 static int ipvideo_decode_block_opcode_0x6_16(IpvideoContext *s, AVFrame *frame)
551 {
552     signed char x, y;
553
554     /* copy a block from the second last frame using an expanded range */
555     x = bytestream2_get_byte(&s->stream_ptr);
556     y = bytestream2_get_byte(&s->stream_ptr);
557
558     ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
559     return copy_from(s, s->second_last_frame, frame, x, y);
560 }
561
562 static int ipvideo_decode_block_opcode_0x7_16(IpvideoContext *s, AVFrame *frame)
563 {
564     int x, y;
565     uint16_t P[2];
566     unsigned int flags;
567     uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
568
569     /* 2-color encoding */
570     P[0] = bytestream2_get_le16(&s->stream_ptr);
571     P[1] = bytestream2_get_le16(&s->stream_ptr);
572
573     if (!(P[0] & 0x8000)) {
574
575         for (y = 0; y < 8; y++) {
576             flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
577             for (; flags != 1; flags >>= 1)
578                 *pixel_ptr++ = P[flags & 1];
579             pixel_ptr += s->line_inc;
580         }
581
582     } else {
583
584         flags = bytestream2_get_le16(&s->stream_ptr);
585         for (y = 0; y < 8; y += 2) {
586             for (x = 0; x < 8; x += 2, flags >>= 1) {
587                 pixel_ptr[x                ] =
588                 pixel_ptr[x + 1            ] =
589                 pixel_ptr[x +     s->stride] =
590                 pixel_ptr[x + 1 + s->stride] = P[flags & 1];
591             }
592             pixel_ptr += s->stride * 2;
593         }
594     }
595
596     return 0;
597 }
598
599 static int ipvideo_decode_block_opcode_0x8_16(IpvideoContext *s, AVFrame *frame)
600 {
601     int x, y;
602     uint16_t P[4];
603     unsigned int flags = 0;
604     uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
605
606     /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
607      * either top and bottom or left and right halves */
608     P[0] = bytestream2_get_le16(&s->stream_ptr);
609     P[1] = bytestream2_get_le16(&s->stream_ptr);
610
611     if (!(P[0] & 0x8000)) {
612
613         for (y = 0; y < 16; y++) {
614             // new values for each 4x4 block
615             if (!(y & 3)) {
616                 if (y) {
617                     P[0] = bytestream2_get_le16(&s->stream_ptr);
618                     P[1] = bytestream2_get_le16(&s->stream_ptr);
619                 }
620                 flags = bytestream2_get_le16(&s->stream_ptr);
621             }
622
623             for (x = 0; x < 4; x++, flags >>= 1)
624                 *pixel_ptr++ = P[flags & 1];
625             pixel_ptr += s->stride - 4;
626             // switch to right half
627             if (y == 7) pixel_ptr -= 8 * s->stride - 4;
628         }
629
630     } else {
631
632         flags = bytestream2_get_le32(&s->stream_ptr);
633         P[2]  = bytestream2_get_le16(&s->stream_ptr);
634         P[3]  = bytestream2_get_le16(&s->stream_ptr);
635
636         if (!(P[2] & 0x8000)) {
637
638             /* vertical split; left & right halves are 2-color encoded */
639
640             for (y = 0; y < 16; y++) {
641                 for (x = 0; x < 4; x++, flags >>= 1)
642                     *pixel_ptr++ = P[flags & 1];
643                 pixel_ptr += s->stride - 4;
644                 // switch to right half
645                 if (y == 7) {
646                     pixel_ptr -= 8 * s->stride - 4;
647                     P[0]  = P[2];
648                     P[1]  = P[3];
649                     flags = bytestream2_get_le32(&s->stream_ptr);
650                 }
651             }
652
653         } else {
654
655             /* horizontal split; top & bottom halves are 2-color encoded */
656
657             for (y = 0; y < 8; y++) {
658                 if (y == 4) {
659                     P[0]  = P[2];
660                     P[1]  = P[3];
661                     flags = bytestream2_get_le32(&s->stream_ptr);
662                 }
663
664                 for (x = 0; x < 8; x++, flags >>= 1)
665                     *pixel_ptr++ = P[flags & 1];
666                 pixel_ptr += s->line_inc;
667             }
668         }
669     }
670
671     /* report success */
672     return 0;
673 }
674
675 static int ipvideo_decode_block_opcode_0x9_16(IpvideoContext *s, AVFrame *frame)
676 {
677     int x, y;
678     uint16_t P[4];
679     uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
680
681     /* 4-color encoding */
682     for (x = 0; x < 4; x++)
683         P[x] = bytestream2_get_le16(&s->stream_ptr);
684
685     if (!(P[0] & 0x8000)) {
686         if (!(P[2] & 0x8000)) {
687
688             /* 1 of 4 colors for each pixel */
689             for (y = 0; y < 8; y++) {
690                 /* get the next set of 8 2-bit flags */
691                 int flags = bytestream2_get_le16(&s->stream_ptr);
692                 for (x = 0; x < 8; x++, flags >>= 2)
693                     *pixel_ptr++ = P[flags & 0x03];
694                 pixel_ptr += s->line_inc;
695             }
696
697         } else {
698             uint32_t flags;
699
700             /* 1 of 4 colors for each 2x2 block */
701             flags = bytestream2_get_le32(&s->stream_ptr);
702
703             for (y = 0; y < 8; y += 2) {
704                 for (x = 0; x < 8; x += 2, flags >>= 2) {
705                     pixel_ptr[x                ] =
706                     pixel_ptr[x + 1            ] =
707                     pixel_ptr[x +     s->stride] =
708                     pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
709                 }
710                 pixel_ptr += s->stride * 2;
711             }
712
713         }
714     } else {
715         uint64_t flags;
716
717         /* 1 of 4 colors for each 2x1 or 1x2 block */
718         flags = bytestream2_get_le64(&s->stream_ptr);
719         if (!(P[2] & 0x8000)) {
720             for (y = 0; y < 8; y++) {
721                 for (x = 0; x < 8; x += 2, flags >>= 2) {
722                     pixel_ptr[x    ] =
723                     pixel_ptr[x + 1] = P[flags & 0x03];
724                 }
725                 pixel_ptr += s->stride;
726             }
727         } else {
728             for (y = 0; y < 8; y += 2) {
729                 for (x = 0; x < 8; x++, flags >>= 2) {
730                     pixel_ptr[x            ] =
731                     pixel_ptr[x + s->stride] = P[flags & 0x03];
732                 }
733                 pixel_ptr += s->stride * 2;
734             }
735         }
736     }
737
738     /* report success */
739     return 0;
740 }
741
742 static int ipvideo_decode_block_opcode_0xA_16(IpvideoContext *s, AVFrame *frame)
743 {
744     int x, y;
745     uint16_t P[8];
746     int flags = 0;
747     uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
748
749     for (x = 0; x < 4; x++)
750         P[x] = bytestream2_get_le16(&s->stream_ptr);
751
752     /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
753      * either top and bottom or left and right halves */
754     if (!(P[0] & 0x8000)) {
755
756         /* 4-color encoding for each quadrant */
757         for (y = 0; y < 16; y++) {
758             // new values for each 4x4 block
759             if (!(y & 3)) {
760                 if (y)
761                     for (x = 0; x < 4; x++)
762                         P[x] = bytestream2_get_le16(&s->stream_ptr);
763                 flags = bytestream2_get_le32(&s->stream_ptr);
764             }
765
766             for (x = 0; x < 4; x++, flags >>= 2)
767                 *pixel_ptr++ = P[flags & 0x03];
768
769             pixel_ptr += s->stride - 4;
770             // switch to right half
771             if (y == 7) pixel_ptr -= 8 * s->stride - 4;
772         }
773
774     } else {
775         // vertical split?
776         int vert;
777         uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
778
779         for (x = 4; x < 8; x++)
780             P[x] = bytestream2_get_le16(&s->stream_ptr);
781         vert = !(P[4] & 0x8000);
782
783         /* 4-color encoding for either left and right or top and bottom
784          * halves */
785
786         for (y = 0; y < 16; y++) {
787             for (x = 0; x < 4; x++, flags >>= 2)
788                 *pixel_ptr++ = P[flags & 0x03];
789
790             if (vert) {
791                 pixel_ptr += s->stride - 4;
792                 // switch to right half
793                 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
794             } else if (y & 1) pixel_ptr += s->line_inc;
795
796             // load values for second half
797             if (y == 7) {
798                 memcpy(P, P + 4, 8);
799                 flags = bytestream2_get_le64(&s->stream_ptr);
800             }
801         }
802     }
803
804     /* report success */
805     return 0;
806 }
807
808 static int ipvideo_decode_block_opcode_0xB_16(IpvideoContext *s, AVFrame *frame)
809 {
810     int x, y;
811     uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
812
813     /* 64-color encoding (each pixel in block is a different color) */
814     for (y = 0; y < 8; y++) {
815         for (x = 0; x < 8; x++)
816             pixel_ptr[x] = bytestream2_get_le16(&s->stream_ptr);
817         pixel_ptr  += s->stride;
818     }
819
820     /* report success */
821     return 0;
822 }
823
824 static int ipvideo_decode_block_opcode_0xC_16(IpvideoContext *s, AVFrame *frame)
825 {
826     int x, y;
827     uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
828
829     /* 16-color block encoding: each 2x2 block is a different color */
830     for (y = 0; y < 8; y += 2) {
831         for (x = 0; x < 8; x += 2) {
832             pixel_ptr[x                ] =
833             pixel_ptr[x + 1            ] =
834             pixel_ptr[x +     s->stride] =
835             pixel_ptr[x + 1 + s->stride] = bytestream2_get_le16(&s->stream_ptr);
836         }
837         pixel_ptr += s->stride * 2;
838     }
839
840     /* report success */
841     return 0;
842 }
843
844 static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s, AVFrame *frame)
845 {
846     int x, y;
847     uint16_t P[2];
848     uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
849
850     /* 4-color block encoding: each 4x4 block is a different color */
851     for (y = 0; y < 8; y++) {
852         if (!(y & 3)) {
853             P[0] = bytestream2_get_le16(&s->stream_ptr);
854             P[1] = bytestream2_get_le16(&s->stream_ptr);
855         }
856         for (x = 0; x < 8; x++)
857             pixel_ptr[x] = P[x >> 2];
858         pixel_ptr += s->stride;
859     }
860
861     /* report success */
862     return 0;
863 }
864
865 static int ipvideo_decode_block_opcode_0xE_16(IpvideoContext *s, AVFrame *frame)
866 {
867     int x, y;
868     uint16_t pix;
869     uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
870
871     /* 1-color encoding: the whole block is 1 solid color */
872     pix = bytestream2_get_le16(&s->stream_ptr);
873
874     for (y = 0; y < 8; y++) {
875         for (x = 0; x < 8; x++)
876             pixel_ptr[x] = pix;
877         pixel_ptr += s->stride;
878     }
879
880     /* report success */
881     return 0;
882 }
883
884 static int (* const ipvideo_decode_block[])(IpvideoContext *s, AVFrame *frame) = {
885     ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
886     ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
887     ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
888     ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
889     ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
890     ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
891     ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
892     ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
893 };
894
895 static int (* const ipvideo_decode_block16[])(IpvideoContext *s, AVFrame *frame) = {
896     ipvideo_decode_block_opcode_0x0,    ipvideo_decode_block_opcode_0x1,
897     ipvideo_decode_block_opcode_0x2,    ipvideo_decode_block_opcode_0x3,
898     ipvideo_decode_block_opcode_0x4,    ipvideo_decode_block_opcode_0x5,
899     ipvideo_decode_block_opcode_0x6_16, ipvideo_decode_block_opcode_0x7_16,
900     ipvideo_decode_block_opcode_0x8_16, ipvideo_decode_block_opcode_0x9_16,
901     ipvideo_decode_block_opcode_0xA_16, ipvideo_decode_block_opcode_0xB_16,
902     ipvideo_decode_block_opcode_0xC_16, ipvideo_decode_block_opcode_0xD_16,
903     ipvideo_decode_block_opcode_0xE_16, ipvideo_decode_block_opcode_0x1,
904 };
905
906 static void ipvideo_decode_opcodes(IpvideoContext *s, AVFrame *frame)
907 {
908     int x, y;
909     unsigned char opcode;
910     int ret;
911     GetBitContext gb;
912
913     bytestream2_skip(&s->stream_ptr, 14); /* data starts 14 bytes in */
914     if (!s->is_16bpp) {
915         /* this is PAL8, so make the palette available */
916         memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
917
918         s->stride = frame->linesize[0];
919     } else {
920         s->stride = frame->linesize[0] >> 1;
921         s->mv_ptr = s->stream_ptr;
922         bytestream2_skip(&s->mv_ptr, bytestream2_get_le16(&s->stream_ptr));
923     }
924     s->line_inc = s->stride - 8;
925     s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
926                                   + (s->avctx->width - 8) * (1 + s->is_16bpp);
927
928     init_get_bits(&gb, s->decoding_map, s->decoding_map_size * 8);
929     for (y = 0; y < s->avctx->height; y += 8) {
930         for (x = 0; x < s->avctx->width; x += 8) {
931             opcode = get_bits(&gb, 4);
932
933             ff_tlog(s->avctx,
934                     "  block @ (%3d, %3d): encoding 0x%X, data ptr offset %d\n",
935                     x, y, opcode, bytestream2_tell(&s->stream_ptr));
936
937             if (!s->is_16bpp) {
938                 s->pixel_ptr = frame->data[0] + x
939                               + y*frame->linesize[0];
940                 ret = ipvideo_decode_block[opcode](s, frame);
941             } else {
942                 s->pixel_ptr = frame->data[0] + x*2
943                               + y*frame->linesize[0];
944                 ret = ipvideo_decode_block16[opcode](s, frame);
945             }
946             if (ret != 0) {
947                 av_log(s->avctx, AV_LOG_ERROR, "decode problem on frame %d, @ block (%d, %d)\n",
948                        s->avctx->frame_number, x, y);
949                 return;
950             }
951         }
952     }
953     if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
954         av_log(s->avctx, AV_LOG_DEBUG,
955                "decode finished with %d bytes left over\n",
956                bytestream2_get_bytes_left(&s->stream_ptr));
957     }
958 }
959
960 static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
961 {
962     IpvideoContext *s = avctx->priv_data;
963
964     s->avctx = avctx;
965
966     s->is_16bpp = avctx->bits_per_coded_sample == 16;
967     avctx->pix_fmt = s->is_16bpp ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_PAL8;
968
969     ff_hpeldsp_init(&s->hdsp, avctx->flags);
970
971     s->last_frame        = av_frame_alloc();
972     s->second_last_frame = av_frame_alloc();
973     if (!s->last_frame || !s->second_last_frame) {
974         av_frame_free(&s->last_frame);
975         av_frame_free(&s->second_last_frame);
976         return AVERROR(ENOMEM);
977     }
978
979     return 0;
980 }
981
982 static int ipvideo_decode_frame(AVCodecContext *avctx,
983                                 void *data, int *got_frame,
984                                 AVPacket *avpkt)
985 {
986     const uint8_t *buf = avpkt->data;
987     int buf_size = avpkt->size;
988     IpvideoContext *s = avctx->priv_data;
989     AVFrame *frame = data;
990     int ret;
991
992     if (av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, NULL)) {
993         av_frame_unref(s->last_frame);
994         av_frame_unref(s->second_last_frame);
995     }
996
997     if (buf_size < 2)
998         return AVERROR_INVALIDDATA;
999
1000     /* decoding map contains 4 bits of information per 8x8 block */
1001     s->decoding_map_size = AV_RL16(avpkt->data);
1002
1003     /* compressed buffer needs to be large enough to at least hold an entire
1004      * decoding map */
1005     if (buf_size < s->decoding_map_size + 2)
1006         return buf_size;
1007
1008
1009     s->decoding_map = buf + 2;
1010     bytestream2_init(&s->stream_ptr, buf + 2 + s->decoding_map_size,
1011                      buf_size - s->decoding_map_size);
1012
1013     if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1014         return ret;
1015
1016     if (!s->is_16bpp) {
1017         int size;
1018         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
1019         if (pal && size == AVPALETTE_SIZE) {
1020             frame->palette_has_changed = 1;
1021             memcpy(s->pal, pal, AVPALETTE_SIZE);
1022         } else if (pal) {
1023             av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
1024         }
1025     }
1026
1027     ipvideo_decode_opcodes(s, frame);
1028
1029     *got_frame = 1;
1030
1031     /* shuffle frames */
1032     av_frame_unref(s->second_last_frame);
1033     FFSWAP(AVFrame*, s->second_last_frame, s->last_frame);
1034     if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
1035         return ret;
1036
1037     /* report that the buffer was completely consumed */
1038     return buf_size;
1039 }
1040
1041 static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
1042 {
1043     IpvideoContext *s = avctx->priv_data;
1044
1045     av_frame_free(&s->last_frame);
1046     av_frame_free(&s->second_last_frame);
1047
1048     return 0;
1049 }
1050
1051 AVCodec ff_interplay_video_decoder = {
1052     .name           = "interplayvideo",
1053     .long_name      = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
1054     .type           = AVMEDIA_TYPE_VIDEO,
1055     .id             = AV_CODEC_ID_INTERPLAY_VIDEO,
1056     .priv_data_size = sizeof(IpvideoContext),
1057     .init           = ipvideo_decode_init,
1058     .close          = ipvideo_decode_end,
1059     .decode         = ipvideo_decode_frame,
1060     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE,
1061 };