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