-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZMBVCodecCore.java
More file actions
1153 lines (1032 loc) · 49 KB
/
ZMBVCodecCore.java
File metadata and controls
1153 lines (1032 loc) · 49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* @(#)ZMBVCodecCore.java 1.0 2011-08-29
*
* Copyright (c) 2011 Werner Randelshofer, Immensee, Switzerland.
* All rights reserved.
*
* You may not use, copy or modify this file, except in compliance with the
* license agreement you entered into with Werner Randelshofer.
* For details see accompanying license terms.
*/
//import com.jcraft.jzlib.InflaterInputStream;
import java.util.zip.InflaterInputStream;
import java.io.IOException;
import java.nio.ByteOrder;
import javax.imageio.stream.ImageInputStream;
import static java.lang.Math.*;
/**
* Implements the DosBox Capture Codec {@code "ZMBV"}.
*
* <p>This is a codec added to the DosBox project to capture screen data
* (like Vmware VMNC).</p>
*
* <p>This codec employs ZLIB compression and has intraframes and delta frames.
* Delta frames seem to have blocks either copied from the previous frame or
* XOR'ed with some block from the previous frame.
* <p>
* The FourCC for this codec is ZMBV which ostensibly stands for Zip Motion
* Blocks Video. The data is most commonly stored in AVI files.
* </p>
* <p><b>Data Format</b></p>
* <p>Byte 0 of a ZMBV data chunk contains the following flags:</p>
* <pre>
* bits 7-2 undefined
* bit 1 palette change
* bit 0 1 = intraframe, 0 = interframe
* </pre>
*
* <p>If the frame is an intra frame as indicated by bit 0 of byte 0, the next
* 6 bytes in the data chunk are formatted as follows:</p>
* <pre>
* byte 1 major version
* byte 2 minor version
* byte 3 compression type (0 = uncompressed, 1 = zlib-compressed)
* byte 4 video format
* byte 5 block width
* byte 6 block height
* </pre>
* <p>Presently, the only valid major/minor version pair is 0/1. A block width or
* height of 0 is invalid. These are the video modes presently defined:</p>
* <pre>
* 0 none
* 1 1 bit/pixel, palettized
* 2 2 bits/pixel, palettized
* 3 4 bits/pixel, palettized
* 4 8 bits/pixel, palettized
* 5 15 bits/pixel
* 6 16 bits/pixel
* 7 24 bits/pixel
* 8 32 bits/pixel
* </pre>
*
* <p>Presently, only modes 4 (8 bpp), 5 (15 bpp), 6 (16 bpp) and 8 (32 bpp) are
* supported.</p>
*
* <p>If the compression type is 1, the remainder of the data chunk is compressed
* using the standard zlib package. Decompress the data before proceeding with
* the next step. Otherwise, proceed to the next step. Also note that you must
* reset zlib for intraframes.</p>
*
* <p>If bit 1 of the frame header (palette change) is set then the first 768
* bytes of the uncompressed data represent 256 red-green-blue palette triplets.
* Each component is one byte and ranges from 0..255.</p>
*
* <p>An intraframe consists of 768 bytes of palette data (for palettized modes)
* and raw frame data.</p>
*
* </p>An interframe is comprised of up to three parts:</p>
* <ol>
* <li>if palette change flag was set then first 768 bytes represent XOR'ed
* palette difference</li>
* <li>block info (2 bytes per block, padded to 4 bytes length)</li>
* <li>block differences</li>
* </ol>
* <p>Block info is composed from a motion vector and a flag: first byte is
* (dx << 1) | flag, second byte is (dy << 1). Motion vectors can go
* out of bounds and in that case you need to zero the out-of-bounds part.
* Also note that currently motion vectors are limited to a range of (-16..16).
* Flag tells whether the codec simply copies the block from the decoded offset
* or copies it and XOR's it with data from block differences. All XORing for
* 15/16 bpp and 32 bpp modes is done with little-endian integers.</p>
*
* <p>Interframe decoding can be done this way:</p>
* <pre>
* for each block {
* a = block_info[current_block][0];
* b = block_info[current_block][1];
* dx = a >> 1;
* dy = b >> 1;
* flag = a & 1;
* copy block from offset (dx, dy) from previous frame.
* if (flag) {
* XOR block with data read from stream.
* }
* }
* </pre>
*
* <p>References<br>
* <a href="http://wiki.multimedia.cx/index.php?title=ZMBV"
* >http://wiki.multimedia.cx/index.php?title=ZMBV</a>
* </p>
*
* <p>Note: We use the JZLib library for decoding compressed input streams,
* because the {@code javax.zip.InflaterInputStream} sometimes fails to decode
* the data.</p>
*
* *
* @author Werner Randelshofer
* @version 1.0 2011-08-29 Created.
*/
public class ZMBVCodecCore {
public final static int VIDEOMODE_NONE = 0;
public final static int VIDEOMODE_1_BIT_PALETTIZED = 1;
public final static int VIDEOMODE_2_BIT_PALETTIZED = 2;
public final static int VIDEOMODE_4_BIT_PALETTIZED = 3;
public final static int VIDEOMODE_8_BIT_PALETTIZED = 4;
public final static int VIDEOMODE_15_BIT_BGR = 5;
public final static int VIDEOMODE_16_BIT_BGR = 6;
public final static int VIDEOMODE_24_BIT_BGR = 7;
public final static int VIDEOMODE_32_BIT_BGR = 8;
public final static int COMPRESSION_NONE = 0;
public final static int COMPRESSION_ZLIB = 1;
/**
*
* @param inDat Input data.
* @param off Input data offset.
* @param length Input data length.
* @param outDat Output data. 32 bits per pixel: {palette index, red, green, blue}.
* @param prevDat Previous output data array. This is needed because the
* codec uses double buffering. 32 bits per pixel: {palette index, red, green, blue}.
* @param width Image width.
* @param height Image height.
* @param state Codec state.
* @return true if keyframe.
*/
private int majorVersion;
private int minorVersion;
private int compressionType;
private int videoFormat;
private int blockWidth, blockHeight;
private InflaterInputStream inflaterInputStream;
private AppendableByteArrayInputStream byteArrayInputStream;
private int[] palette;
private byte[] blockDataBuf;
private byte[] blockHeaderBuf;
/** Decodes to 32-bit RGB.
* Returns true if a key-frame was decoded.
*/
public boolean decode(byte[] inDat, int off, int length, int[] outDat, int[] prevDat, int width, int height, boolean onlyDecodeIfKeyframe) {
boolean isKeyframe = false;
try {
ImageInputStream in = new ByteArrayImageInputStream(inDat, off, length, ByteOrder.LITTLE_ENDIAN);
int flags = in.readUnsignedByte();
isKeyframe = (flags & 1) != 0;
if (onlyDecodeIfKeyframe && !isKeyframe) {
System.out.println("ZMBVCodec cannot decode delta without preceeding keyframe.");
return false;
}
if (isKeyframe) {
// => Key frame
//System.out.println("ZMBVCode Keyframe w,h=" + width + "," + height);
majorVersion = in.readUnsignedByte();
minorVersion = in.readUnsignedByte();
compressionType = in.readUnsignedByte();
videoFormat = in.readUnsignedByte();
blockWidth = in.readUnsignedByte();
blockHeight = in.readUnsignedByte();
}
if (majorVersion != 0 || minorVersion != 1) {
System.err.println("unsupported version " + majorVersion + "." + minorVersion);
return isKeyframe;
}
switch (compressionType) {
case COMPRESSION_ZLIB:
if (!isKeyframe && inflaterInputStream != null) {
// => streams are present.
// Append new data.
AppendableByteArrayInputStream bais = byteArrayInputStream;
bais.appendBuffer(inDat, (int) in.getStreamPosition() + off, (int) (length - in.getStreamPosition()), true);
} else {
// => Keyframe or no Inflater Stream present. Create new one, and ensure
// that we can append new data to it later on.
if (byteArrayInputStream != null) {
byteArrayInputStream.setBuffer(inDat, (int) in.getStreamPosition() + off, (int) (length - in.getStreamPosition()));
} else {
byteArrayInputStream = new AppendableByteArrayInputStream(inDat.clone(), (int) in.getStreamPosition() + off, (int) (length - in.getStreamPosition()));
}
if (inflaterInputStream != null) {
inflaterInputStream.close();
}
inflaterInputStream = new InflaterInputStream(byteArrayInputStream);
}
in = new UncachedImageInputStream(inflaterInputStream, ByteOrder.LITTLE_ENDIAN);
break;
case COMPRESSION_NONE:
System.out.println(" NO COMPRESSION");
return isKeyframe;
default:
System.err.println("unsupported compression type " + compressionType);
return isKeyframe;
}
switch (videoFormat) {
case VIDEOMODE_8_BIT_PALETTIZED:
decode8to32(in, outDat, prevDat, flags, width, height);
break;
case VIDEOMODE_15_BIT_BGR:
decode15to32(in, outDat, prevDat, flags, width, height);
break;
case VIDEOMODE_16_BIT_BGR:
decode16to32(in, outDat, prevDat, flags, width, height);
break;
case VIDEOMODE_32_BIT_BGR:
decode32to32(in, outDat, prevDat, flags, width, height);
break;
default:
throw new UnsupportedOperationException("Unsupported video format " + videoFormat);
}
} catch (IOException ex) {
//System.out.println("ZMBVCodecCore "+ex);
System.err.println("ZMBVCodecCore decoding, isKeyframe=" + isKeyframe);
ex.printStackTrace();
}
return isKeyframe;
}
/** Decodes to 8-bit palettised.
* Returns true if a key-frame was decoded.
*/
public boolean decode(byte[] inDat, int off, int length, byte[] outDat, byte[] prevDat, int width, int height, boolean onlyDecodeIfKeyframe) {
boolean isKeyframe = false;
try {
ImageInputStream in = new ByteArrayImageInputStream(inDat, off, length, ByteOrder.LITTLE_ENDIAN);
int flags = in.readUnsignedByte();
isKeyframe = (flags & 1) != 0;
if (onlyDecodeIfKeyframe && !isKeyframe) {
System.out.println("ZMBVCodec cannot decode delta without preceeding keyframe.");
return false;
}
if (isKeyframe) {
// => Key frame
//System.out.println("ZMBVCode Keyframe w,h=" + width + "," + height);
majorVersion = in.readUnsignedByte();
minorVersion = in.readUnsignedByte();
compressionType = in.readUnsignedByte();
videoFormat = in.readUnsignedByte();
blockWidth = in.readUnsignedByte();
blockHeight = in.readUnsignedByte();
}
if (majorVersion != 0 || minorVersion != 1) {
System.err.println("unsupported version " + majorVersion + "." + minorVersion);
return isKeyframe;
}
switch (compressionType) {
case COMPRESSION_ZLIB:
if (!isKeyframe && inflaterInputStream != null) {
// => streams are present.
// Append new data.
AppendableByteArrayInputStream bais = byteArrayInputStream;
bais.appendBuffer(inDat, (int) in.getStreamPosition() + off, (int) (length - in.getStreamPosition()), true);
} else {
// => Keyframe or no Inflater Stream present. Create new one, and ensure
// that we can append new data to it later on.
if (byteArrayInputStream != null) {
byteArrayInputStream.setBuffer(inDat, (int) in.getStreamPosition() + off, (int) (length - in.getStreamPosition()));
} else {
byteArrayInputStream = new AppendableByteArrayInputStream(inDat.clone(), (int) in.getStreamPosition() + off, (int) (length - in.getStreamPosition()));
}
if (inflaterInputStream != null) {
inflaterInputStream.close();
}
inflaterInputStream = new InflaterInputStream(byteArrayInputStream);
}
in = new UncachedImageInputStream(inflaterInputStream, ByteOrder.LITTLE_ENDIAN);
break;
case COMPRESSION_NONE:
System.out.println(" NO COMPRESSION");
return isKeyframe;
default:
System.err.println("unsupported compression type " + compressionType);
return isKeyframe;
}
switch (videoFormat) {
case VIDEOMODE_8_BIT_PALETTIZED:
decode8to8(in, outDat, prevDat, flags, width, height);
break;
/*
case VIDEOMODE_15_BIT_BGR:
decode15BitBGR(in, outDat, prevDat, flags, width, height);
break;
case VIDEOMODE_16_BIT_BGR:
decode16BitBGR(in, outDat, prevDat, flags, width, height);
break;
case VIDEOMODE_32_BIT_BGR:
decode32BitBGR(in, outDat, prevDat, flags, width, height);
break;
* */
default:
throw new UnsupportedOperationException("Unsupported video format " + videoFormat);
}
} catch (IOException ex) {
//System.out.println("ZMBVCodecCore "+ex);
System.err.println("ZMBVCodecCore decoding, isKeyframe=" + isKeyframe);
ex.printStackTrace();
}
return isKeyframe;
}
/** Decodes to 8-bit, 15-bit, 16-bit or 32-bit RGB depending on input data.
* Returns the number of decoded bits.
* Returns a negative number if keyframe.
* Returns 0 in case of failure.
*/
public int decode(byte[] inDat, int off, int length, Object[] outDatHolder, Object[] prevDatHolder, int width, int height, boolean onlyDecodeIfKeyframe) {
boolean isKeyframe = false;
int depth = 0;
try {
ImageInputStream in = new ByteArrayImageInputStream(inDat, off, length, ByteOrder.LITTLE_ENDIAN);
int flags = in.readUnsignedByte();
isKeyframe = (flags & 1) != 0;
if (onlyDecodeIfKeyframe && !isKeyframe) {
System.err.println("ZMBVCodec cannot decode delta without preceeding keyframe.");
return 0;
}
if (isKeyframe) {
// => Key frame
//System.out.println("ZMBVCode Keyframe w,h=" + width + "," + height);
majorVersion = in.readUnsignedByte();
minorVersion = in.readUnsignedByte();
compressionType = in.readUnsignedByte();
videoFormat = in.readUnsignedByte();
blockWidth = in.readUnsignedByte();
blockHeight = in.readUnsignedByte();
}
if (majorVersion != 0 || minorVersion != 1) {
System.err.println("unsupported version " + majorVersion + "." + minorVersion);
return 0;
}
switch (compressionType) {
case COMPRESSION_ZLIB:
if (!isKeyframe && inflaterInputStream != null) {
// => streams are present.
// Append new data.
AppendableByteArrayInputStream bais = byteArrayInputStream;
bais.appendBuffer(inDat, (int) in.getStreamPosition() + off, (int) (length - in.getStreamPosition()), true);
} else {
// => Keyframe or no Inflater Stream present. Create new one, and ensure
// that we can append new data to it later on.
if (byteArrayInputStream != null) {
byteArrayInputStream.setBuffer(inDat, (int) in.getStreamPosition() + off, (int) (length - in.getStreamPosition()));
} else {
byteArrayInputStream = new AppendableByteArrayInputStream(inDat.clone(), (int) in.getStreamPosition() + off, (int) (length - in.getStreamPosition()));
}
if (inflaterInputStream != null) {
inflaterInputStream.close();
}
inflaterInputStream = new InflaterInputStream(byteArrayInputStream);
}
in = new UncachedImageInputStream(inflaterInputStream, ByteOrder.LITTLE_ENDIAN);
break;
case COMPRESSION_NONE:
System.err.println(" NO COMPRESSION");
return 0;
default:
System.err.println("unsupported compression type " + compressionType);
return 0;
}
switch (videoFormat) {
case VIDEOMODE_8_BIT_PALETTIZED:
depth = 8;
if (!(outDatHolder[0] instanceof byte[])) {
outDatHolder[0] = new byte[width * height];
}
if (!(prevDatHolder[0] instanceof byte[])) {
prevDatHolder[0] = new byte[width * height];
}
decode8to8(in, (byte[]) outDatHolder[0], (byte[]) prevDatHolder[0], flags, width, height);
break;
case VIDEOMODE_15_BIT_BGR:
depth = 15;
if (!(outDatHolder[0] instanceof short[])) {
outDatHolder[0] = new short[width * height];
}
if (!(prevDatHolder[0] instanceof short[])) {
prevDatHolder[0] = new short[width * height];
}
decode15to15(in, (short[]) outDatHolder[0], (short[]) prevDatHolder[0], flags, width, height);
break;
case VIDEOMODE_16_BIT_BGR:
depth = 16;
if (!(outDatHolder[0] instanceof short[])) {
outDatHolder[0] = new short[width * height];
}
if (!(prevDatHolder[0] instanceof short[])) {
prevDatHolder[0] = new short[width * height];
}
decode16to16(in, (short[]) outDatHolder[0], (short[]) prevDatHolder[0], flags, width, height);
break;
case VIDEOMODE_32_BIT_BGR:
depth = 32;
if (!(outDatHolder[0] instanceof int[])) {
outDatHolder[0] = new short[width * height];
}
if (!(prevDatHolder[0] instanceof int[])) {
prevDatHolder[0] = new short[width * height];
}
decode32to32(in, (int[]) outDatHolder[0], (int[]) prevDatHolder[0], flags, width, height);
break;
default:
throw new UnsupportedOperationException("Unsupported video format " + videoFormat);
}
} catch (IOException ex) {
//System.out.println("ZMBVCodecCore "+ex);
System.err.println("ZMBVCodecCore decoding, isKeyframe=" + isKeyframe);
ex.printStackTrace();
}
return isKeyframe ? -depth : depth;
}
private void decode8to32(ImageInputStream in, int[] outDat, int[] prevDat, int flags, int width, int height) throws IOException {
boolean isKeyframe = (flags & 1) != 0;
boolean isPaletteChange = (flags & 2) != 0;
// palette each entry contains a 32-bit entry constisting of:
// {palette index, red, green, blue}.
if (palette == null) {
palette = new int[256];
}
int blockSize = blockWidth * blockHeight;
if (blockDataBuf == null || blockDataBuf.length < max(3, blockSize)) {
blockDataBuf = new byte[max(3, blockSize)];
}
byte[] buf = blockDataBuf;
if (isKeyframe) {
// => Key frame.
// Read palette
for (int i = 0; i < 256; i++) {
in.readFully(buf, 0, 3);
palette[i] = ((buf[2] & 0xff)) | ((buf[1] & 0xff) << 8) | ((buf[0] & 0xff) << 16) | (i << 24);
}
// Process raw pixels
for (int i = 0, n = width * height; i < n; i++) {
outDat[i] = palette[in.readUnsignedByte()];
}
} else {
// => Delta frame.
// Optionally update palette
if (isPaletteChange) {
for (int i = 0; i < 256; i++) {
in.readFully(buf, 0, 3);
palette[i] ^= ((buf[2] & 0xff)) | ((buf[1] & 0xff) << 8) | ((buf[0] & 0xff) << 16);
}
}
// Read block headers
int nbx = (width + blockWidth - 1) / blockWidth;
int nby = (height + blockHeight - 1) / blockHeight;
int blockHeaderSize = ((nbx * nby * 2 + 3) & ~3);
if (blockHeaderBuf == null || blockHeaderBuf.length < blockHeaderSize) {
blockHeaderBuf = new byte[blockHeaderSize];
}
byte[] blocks = blockHeaderBuf;
in.readFully(blocks, 0, blockHeaderSize);
// Process block data
int block = 0;
for (int by = 0; by < height; by += blockHeight) {
int bh2 = min(height - by, blockHeight);
for (int bx = 0; bx < width; bx += blockWidth) {
int bw2 = min(width - bx, blockWidth);
int a = blocks[block++];
int b = blocks[block++];
int dx = a >> 1;
int dy = b >> 1;
int flag = a & 1;
if (flag == 0) {
// => copy block from offset dx,dy from previous frame
// motion vectors out of bounds are used to zero blocks.
for (int y = 0; y < bh2; y++) {
int py = by + y + dy;
int iout = bx + (by + y) * width;
if (py < 0 || height <= py) {
for (int x = 0; x < bw2; x++) {
outDat[iout++] = palette[0];
}
} else {
for (int x = 0; x < bw2; x++) {
int px = bx + x + dx;
if (0 <= px && px < width) {
outDat[iout++] = palette[prevDat[px + py * width] >>> 24];
} else {
outDat[iout++] = palette[0];
}
}
}
}
} else {
// => XOR block with data read from stream
in.readFully(buf, 0, bw2 * bh2);
int iblock = 0;
for (int y = 0; y < bh2; y++) {
int py = by + y + dy;
int iout = bx + (by + y) * width;
for (int x = 0; x < bw2; x++) {
int px = bx + x + dx;
int paletteIndex = buf[iblock++] & 0xff;
if (0 <= py && py < height && 0 <= px && px < width) {
paletteIndex ^= prevDat[px + py * width] >>> 24;
}
outDat[iout] = palette[paletteIndex];
iout++;
}
}
}
}
}
}
}
private void decode8to8(ImageInputStream in, byte[] outDat, byte[] prevDat, int flags, int width, int height) throws IOException {
boolean isKeyframe = (flags & 1) != 0;
boolean isPaletteChange = (flags & 2) != 0;
// palette each entry contains a 32-bit entry constisting of:
// {palette index, red, green, blue}.
if (palette == null) {
palette = new int[256];
}
int blockSize = blockWidth * blockHeight;
if (blockDataBuf == null || blockDataBuf.length < max(3, blockSize)) {
blockDataBuf = new byte[max(3, blockSize)];
}
byte[] buf = blockDataBuf;
if (isKeyframe) {
// => Key frame.
// Read palette
for (int i = 0; i < 256; i++) {
in.readFully(buf, 0, 3);
palette[i] = ((buf[2] & 0xff)) | ((buf[1] & 0xff) << 8) | ((buf[0] & 0xff) << 16) | (i << 24);
}
// Process raw pixels
for (int i = 0, n = width * height; i < n; i++) {
outDat[i] = in.readByte();
}
} else {
// => Delta frame.
// Optionally update palette
if (isPaletteChange) {
for (int i = 0; i < 256; i++) {
in.readFully(buf, 0, 3);
palette[i] ^= ((buf[2] & 0xff)) | ((buf[1] & 0xff) << 8) | ((buf[0] & 0xff) << 16);
}
}
// Read block headers
int nbx = (width + blockWidth - 1) / blockWidth;
int nby = (height + blockHeight - 1) / blockHeight;
int blockHeaderSize = ((nbx * nby * 2 + 3) & ~3);
if (blockHeaderBuf == null || blockHeaderBuf.length < blockHeaderSize) {
blockHeaderBuf = new byte[blockHeaderSize];
}
byte[] blocks = blockHeaderBuf;
in.readFully(blocks, 0, blockHeaderSize);
// Process block data
int block = 0;
for (int by = 0; by < height; by += blockHeight) {
int bh2 = min(height - by, blockHeight);
for (int bx = 0; bx < width; bx += blockWidth) {
int bw2 = min(width - bx, blockWidth);
int a = blocks[block++];
int b = blocks[block++];
int dx = a >> 1;
int dy = b >> 1;
int flag = a & 1;
if (flag == 0) {
// => copy block from offset dx,dy from previous frame
// motion vectors out of bounds are used to zero blocks.
for (int y = 0; y < bh2; y++) {
int py = by + y + dy;
int iout = bx + (by + y) * width;
if (py < 0 || height <= py) {
for (int x = 0; x < bw2; x++) {
outDat[iout++] = 0;
}
} else {
for (int x = 0; x < bw2; x++) {
int px = bx + x + dx;
if (0 <= px && px < width) {
outDat[iout++] = prevDat[px + py * width];
} else {
outDat[iout++] = 0;
}
}
}
}
} else {
// => XOR block with data read from stream
in.readFully(buf, 0, bw2 * bh2);
int iblock = 0;
for (int y = 0; y < bh2; y++) {
int py = by + y + dy;
int iout = bx + (by + y) * width;
for (int x = 0; x < bw2; x++) {
int px = bx + x + dx;
byte paletteIndex = buf[iblock++];
if (0 <= py && py < height && 0 <= px && px < width) {
paletteIndex ^= prevDat[px + py * width];
}
outDat[iout] = paletteIndex;
iout++;
}
}
}
}
}
}
}
private void decode15to32(ImageInputStream in, int[] outDat, int[] prevDat, int flags, int width, int height) throws IOException {
boolean isKeyframe = (flags & 1) != 0;
int blockSize = blockWidth * blockHeight;
if (blockDataBuf == null || blockDataBuf.length < max(3, blockSize * 2)) {
blockDataBuf = new byte[max(3, blockSize * 2)];
}
byte[] buf = blockDataBuf;
if (isKeyframe) {
// => Key frame.
// Process raw pixels
for (int i = 0, n = width * height; i < n; i++) {
int bgr = in.readUnsignedShort();
outDat[i] = ((bgr & (0x1f << 5)) << 6) | ((bgr & (0x1c << 5)) << 1)//green
| ((bgr & (0x1f << 10)) << 9) | ((bgr & (0x1c << 10)) << 4) // red
| ((bgr & (0x1f << 0)) << 3) | ((bgr & (0x1c << 0)) >>> 2) // blue
;
}
} else {
// => Delta frame.
// Read block headers
int nbx = (width + blockWidth - 1) / blockWidth;
int nby = (height + blockHeight - 1) / blockHeight;
int blockHeaderSize = ((nbx * nby * 2 + 3) & ~3);
//System.out.println("blockHeaderSize=" + blockHeaderSize + " blockSize x,y=" + blockWidth + "," + blockHeight);
if (blockHeaderBuf == null || blockHeaderBuf.length < blockHeaderSize) {
blockHeaderBuf = new byte[blockHeaderSize];
}
byte[] blocks = blockHeaderBuf;
in.readFully(blocks, 0, blockHeaderSize);
// Process block data
int block = 0;
for (int by = 0; by < height; by += blockHeight) {
int bh2 = min(height - by, blockHeight);
for (int bx = 0; bx < width; bx += blockWidth) {
int bw2 = min(width - bx, blockWidth);
int a = blocks[block++];
int b = blocks[block++];
int dx = a >> 1;
int dy = b >> 1;
int flag = a & 1;
if (flag == 0) {
// => copy block from offset dx,dy from previous frame
// motion vectors out of bounds are used to zero blocks.
for (int y = 0; y < bh2; y++) {
int py = by + y + dy;
int iout = bx + (by + y) * width;
if (py < 0 || height <= py) {
for (int x = 0; x < bw2; x++) {
outDat[iout++] = 0;
}
} else {
for (int x = 0; x < bw2; x++) {
int px = bx + x + dx;
if (0 <= px && px < width) {
outDat[iout++] = prevDat[px + py * width];
} else {
outDat[iout++] = 0;
}
}
}
}
} else {
// => XOR block with data read from stream
in.readFully(buf, 0, bw2 * bh2 * 2);
int iblock = 0;
for (int y = 0; y < bh2; y++) {
int py = by + y + dy;
int iout = bx + (by + y) * width;
for (int x = 0; x < bw2; x++) {
int px = bx + x + dx;
int bgr = ((buf[iblock++] & 0xff)) | ((buf[iblock++] & 0xff) << 8);
int rgb = ((bgr & (0x1f << 5)) << 6) | ((bgr & (0x1c << 5)) << 1)//green
| ((bgr & (0x1f << 10)) << 9) | ((bgr & (0x1c << 10)) << 4) // red
| ((bgr & (0x1f << 0)) << 3) | ((bgr & (0x1c << 0)) >>> 2) // blue
;
if (0 <= py && py < height && 0 <= px && px < width) {
rgb ^= prevDat[px + py * width];
}
outDat[iout] = rgb;
iout++;
}
}
}
}
}
}
}
private void decode15to15(ImageInputStream in, short[] outDat, short[] prevDat, int flags, int width, int height) throws IOException {
boolean isKeyframe = (flags & 1) != 0;
int blockSize = blockWidth * blockHeight;
if (blockDataBuf == null || blockDataBuf.length < max(3, blockSize * 2)) {
blockDataBuf = new byte[max(3, blockSize * 2)];
}
byte[] buf = blockDataBuf;
if (isKeyframe) {
// => Key frame.
// Process raw pixels
for (int i = 0, n = width * height; i < n; i++) {
int bgr = in.readUnsignedShort();
outDat[i] = (short) bgr;
}
} else {
// => Delta frame.
// Read block headers
int nbx = (width + blockWidth - 1) / blockWidth;
int nby = (height + blockHeight - 1) / blockHeight;
int blockHeaderSize = ((nbx * nby * 2 + 3) & ~3);
//System.out.println("blockHeaderSize=" + blockHeaderSize + " blockSize x,y=" + blockWidth + "," + blockHeight);
if (blockHeaderBuf == null || blockHeaderBuf.length < blockHeaderSize) {
blockHeaderBuf = new byte[blockHeaderSize];
}
byte[] blocks = blockHeaderBuf;
in.readFully(blocks, 0, blockHeaderSize);
// Process block data
int block = 0;
for (int by = 0; by < height; by += blockHeight) {
int bh2 = min(height - by, blockHeight);
for (int bx = 0; bx < width; bx += blockWidth) {
int bw2 = min(width - bx, blockWidth);
int a = blocks[block++];
int b = blocks[block++];
int dx = a >> 1;
int dy = b >> 1;
int flag = a & 1;
if (flag == 0) {
// => copy block from offset dx,dy from previous frame
// motion vectors out of bounds are used to zero blocks.
for (int y = 0; y < bh2; y++) {
int py = by + y + dy;
int iout = bx + (by + y) * width;
if (py < 0 || height <= py) {
for (int x = 0; x < bw2; x++) {
outDat[iout++] = 0;
}
} else {
for (int x = 0; x < bw2; x++) {
int px = bx + x + dx;
if (0 <= px && px < width) {
outDat[iout++] = prevDat[px + py * width];
} else {
outDat[iout++] = 0;
}
}
}
}
} else {
// => XOR block with data read from stream
in.readFully(buf, 0, bw2 * bh2 * 2);
int iblock = 0;
for (int y = 0; y < bh2; y++) {
int py = by + y + dy;
int iout = bx + (by + y) * width;
for (int x = 0; x < bw2; x++) {
int px = bx + x + dx;
int bgr = ((buf[iblock++] & 0xff)) | ((buf[iblock++] & 0xff) << 8);
if (0 <= py && py < height && 0 <= px && px < width) {
bgr ^= prevDat[px + py * width];
}
outDat[iout] = (short) bgr;
iout++;
}
}
}
}
}
}
}
private void decode16to32(ImageInputStream in, int[] outDat, int[] prevDat, int flags, int width, int height) throws IOException {
boolean isKeyframe = (flags & 1) != 0;
int blockSize = blockWidth * blockHeight;
if (blockDataBuf == null || blockDataBuf.length < max(3, blockSize * 2)) {
blockDataBuf = new byte[max(3, blockSize * 2)];
}
byte[] buf = blockDataBuf;
if (isKeyframe) {
// => Key frame.
// Process raw pixels
for (int i = 0, n = width * height; i < n; i++) {
int bgr = in.readUnsignedShort();
outDat[i] = ((bgr & (0x3f << 5)) << 5) | ((bgr & (0x30 << 5)) >> 1)//green
| ((bgr & (0x1f << 11)) << 8) | ((bgr & (0x1c << 11)) << 3) // red
| ((bgr & (0x1f << 0)) << 3) | ((bgr & (0x1c << 0)) >>> 2) // blue
;
}
} else {
// => Delta frame.
// Read block headers
int nbx = (width + blockWidth - 1) / blockWidth;
int nby = (height + blockHeight - 1) / blockHeight;
int blockHeaderSize = ((nbx * nby * 2 + 3) & ~3);
if (blockHeaderBuf == null || blockHeaderBuf.length < blockHeaderSize) {
blockHeaderBuf = new byte[blockHeaderSize];
}
byte[] blocks = blockHeaderBuf;
in.readFully(blocks, 0, blockHeaderSize);
// Process block data
int block = 0;
for (int by = 0; by < height; by += blockHeight) {
int bh2 = min(height - by, blockHeight);
for (int bx = 0; bx < width; bx += blockWidth) {
int bw2 = min(width - bx, blockWidth);
int a = blocks[block++];
int b = blocks[block++];
int dx = a >> 1;
int dy = b >> 1;
int flag = a & 1;
if (flag == 0) {
// => copy block from offset dx,dy from previous frame
for (int y = 0; y < bh2; y++) {
int py = by + y + dy;
int iout = bx + (by + y) * width;
for (int x = 0; x < bw2; x++) {
int px = bx + x + dx;
if (0 <= py && py < height && 0 <= px && px < width) {
outDat[iout] = prevDat[px + py * width];
} else {
outDat[iout] = 0;
}
iout++;
}
}
} else {
// => XOR block with data read from stream
in.readFully(buf, 0, bw2 * bh2 * 2);
int iblock = 0;
for (int y = 0; y < bh2; y++) {
int py = by + y + dy;
int iout = bx + (by + y) * width;
for (int x = 0; x < bw2; x++) {
int px = bx + x + dx;
int bgr = ((buf[iblock++] & 0xff)) | ((buf[iblock++] & 0xff) << 8);
int rgb = ((bgr & (0x3f << 5)) << 5) | ((bgr & (0x30 << 5)) >> 1)//green
| ((bgr & (0x1f << 11)) << 8) | ((bgr & (0x1c << 11)) << 3) // red
| ((bgr & (0x1f << 0)) << 3) | ((bgr & (0x1c << 0)) >>> 2) // blue
;
if (0 <= py && py < height && 0 <= px && px < width) {
rgb ^= prevDat[px + py * width];
}
outDat[iout] = (short) bgr;
iout++;
}
}
}
}
}
}
}
private void decode16to16(ImageInputStream in, short[] outDat, short[] prevDat, int flags, int width, int height) throws IOException {
boolean isKeyframe = (flags & 1) != 0;
int blockSize = blockWidth * blockHeight;
if (blockDataBuf == null || blockDataBuf.length < max(3, blockSize * 2)) {
blockDataBuf = new byte[max(3, blockSize * 2)];
}
byte[] buf = blockDataBuf;
if (isKeyframe) {
// => Key frame.
// Process raw pixels
for (int i = 0, n = width * height; i < n; i++) {
int bgr = in.readUnsignedShort();
outDat[i] = (short) bgr;
}
} else {
// => Delta frame.
// Read block headers
int nbx = (width + blockWidth - 1) / blockWidth;
int nby = (height + blockHeight - 1) / blockHeight;
int blockHeaderSize = ((nbx * nby * 2 + 3) & ~3);
if (blockHeaderBuf == null || blockHeaderBuf.length < blockHeaderSize) {
blockHeaderBuf = new byte[blockHeaderSize];
}
byte[] blocks = blockHeaderBuf;
in.readFully(blocks, 0, blockHeaderSize);
// Process block data
int block = 0;
for (int by = 0; by < height; by += blockHeight) {
int bh2 = min(height - by, blockHeight);
for (int bx = 0; bx < width; bx += blockWidth) {
int bw2 = min(width - bx, blockWidth);
int a = blocks[block++];
int b = blocks[block++];
int dx = a >> 1;