forked from thorfdbg/codestream-parser
-
Notifications
You must be signed in to change notification settings - Fork 3
/
jp2file.py
executable file
·2393 lines (2267 loc) · 85.2 KB
/
jp2file.py
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
#!/usr/bin/python
# $Id: jp2file.py,v 1.79 2023/10/19 09:56:36 thor Exp $
import getopt
import sys
import jxrfile
import jpgcodestream
import jxscodestream
import jp2codestream
from jp2box import *
from jp2utils import *
from icc import *
def parse_resolution_box(box,buffer):
if len(buffer) != 10:
box.print_ident("invalid box")
return
vrn = ordw(buffer[0:2])
vrd = ordw(buffer[2:4])
hrn = ordw(buffer[4:6])
hrd = ordw(buffer[6:8])
vre = buffer[8]
hre = buffer[9]
box.print_indent("Resolution: %d/%d*10^%d x %d/%d*10^%d" % \
(hrn, hrd, hre, vrn, vrd, vre))
def parse_placeholder_box(box,buffer):
print("Placeholder box")
if len(buffer) < 6+8:
box.print_indent("invalid box")
return
flags = ord(buffer[3])
hdr = "Access to original box"
if flags & 1:
box.print_indent("%s : yes" % hdr)
else:
box.print_indent("%s : no " % hdr)
hdr = "Stream equivalent box "
if flags & 2:
box.print_indent("%s : yes" % hdr)
else:
box.print_indent("%s : no " % hdr)
hdr = "Incremental codestream"
if flags & 4:
if flags & 8:
box.print_indent("%s : yes (many)" % hdr)
else:
box.print_indent("%s : yes (one)" % hdr)
else:
box.print_indent("%s : no" % hdr)
binid = ordq(buffer[4:12])
box.print_indent("Contents in bin : %d" % binid)
hdr = box.parse_string_header(buffer[12:len(buffer)])
buffer = hdr[0]
box.print_indent("Original box length : %d" % hdr[1])
box.print_indent("Original box type : %s" % hdr[2])
if flags & 6:
binid = ordq(buffer[0:8])
box.print_indent("Stream equiv. in bin : %d" % binid)
hdr = box.parse_string_header(buffer[8:len(buffer)])
buffer = hdr[0]
box.print_indent("Stream equiv. box len : %d" % hdr[1])
box.print_indent("Stream equiv. box type : %s" % hdr[2])
if flags & 4:
csn = ordq(buffer[0:8])
box.print_indent("Codestream index : %d" % csn)
if flags & 8:
num = ordl(buffer[0:4])
box.print_indent("Number of codestreams : %d" % num)
def parse_rreq_box(box,buffer):
print("Reader Requirements Box")
if len(buffer) < 5:
box.print_indent("invalid box")
ml = ord(buffer[0])
box.print_indent("Mask length %d" % ord(buffer[0]))
off = 1
box.print_indent("Fully Understand Mask :",0)
for i in range(ml):
print("0x%02x " % ord(buffer[off]), end=' ')
off += 1
print()
box.print_indent("Display Contents Mask :",0)
for i in range(ml):
print("0x%02x " % ord(buffer[off]), end=' ')
off += 1
print()
nsf = ordw(buffer[off:off+2])
box.print_indent("Number of standard flags: %d" % nsf)
off += 2
for i in range(nsf):
sf = ordw(buffer[off:off+2])
off += 2
box.print_indent(" Standard flag : ",0)
if sf == 0:
print("writer could not fully understand file")
elif sf == 1:
print("no extensions (dep)")
elif sf == 2:
print("multiple composition layers")
elif sf == 3:
print("profile 0 (dep)")
elif sf == 4:
print("profile 1")
elif sf == 5:
print("full profile")
elif sf == 6:
print("JPEG 2000-2")
elif sf == 7:
print("DCT")
elif sf == 8:
print("no opacity (dep)")
elif sf == 9:
print("non-premultiplied opacity")
elif sf == 10:
print("premultiplied opacity")
elif sf == 11:
print("chroma-key opacity")
elif sf == 12:
print("contigous codestream (dep)")
elif sf == 13:
print("fragmented in-order codestream")
elif sf == 14:
print("fragmented out-of-order codestream")
elif sf == 15:
print("fragments in multiple local files")
elif sf == 16:
print("fragments accross the internet")
elif sf == 17:
print("using composition")
elif sf == 18:
print("compositing layer support not required (dep)")
elif sf == 19:
print("contains multiple layers (dep)")
elif sf == 20:
print("each layer contains only a single codestream (dep)")
elif sf == 21:
print("layers contain multiple codestreams (dep)")
elif sf == 22:
print("all layers in the same colorspace")
elif sf == 23:
print("layers in multiple colorspaces")
elif sf == 24:
print("animation not required (dep)")
elif sf == 25:
print("animated, but first layer covers entire area and is opaque")
elif sf == 26:
print("animated, but first layer does not cover entire area")
elif sf == 27:
print("animated, and no layer reused (dep)")
elif sf == 28:
print("animated, but layers are reused")
elif sf == 29:
print("animated with persistent frames only (dep)")
elif sf == 30:
print("animated without persistent frames only")
elif sf == 31:
print("no scaling required (dep)")
elif sf == 32:
print("scaling within a layer required")
elif sf == 33:
print("scaling between layer required")
elif sf == 34:
print("contains ROI metadata")
elif sf == 35:
print("contains IPR metadata")
elif sf == 36:
print("contains content metadata")
elif sf == 37:
print("contains history metadata")
elif sf == 38:
print("contains creation metadata")
elif sf == 39:
print("digitally signed")
elif sf == 40:
print("is checksummed")
elif sf == 41:
print("desired Graphic Arts reproduction specified")
elif sf == 42:
print("palettized colors (dep)")
elif sf == 43:
print("Restricted ICC color profiles (dep)")
elif sf == 44:
print("Any ICC color profiles")
elif sf == 45:
print("sRGB (dep)")
elif sf == 46:
print("sRGB-grey (dep)")
elif sf == 47:
print("BiLevel 1")
elif sf == 48:
print("BiLevel 2")
elif sf == 49:
print("YCbCr 1")
elif sf == 50:
print("YCbCr 2")
elif sf == 51:
print("YCbCr 3")
elif sf == 52:
print("PhotoYCC")
elif sf == 53:
print("YCCK")
elif sf == 54:
print("CMY")
elif sf == 55:
print("CMYK")
elif sf == 56:
print("CIELab (default)")
elif sf == 57:
print("CIELab with parameters")
elif sf == 58:
print("CIEJab (default)")
elif sf == 59:
print("CIEJab with parameters")
elif sf == 60:
print("e-sRGB")
elif sf == 61:
print("ROMM-RGB")
elif sf == 62:
print("non-square samples")
elif sf == 63:
print("layers have labels (dep)")
elif sf == 64:
print("codestreams have labels (dep)")
elif sf == 66:
print("layers have different metadata (dep)")
elif sf == 67:
print("GIS metadata XML box")
elif sf == 68:
print("JPSEC extensions")
elif sf == 69:
print("JP3D extensions")
elif sf == 71:
print("e-sYCC")
elif sf == 72:
print("JPX baseline")
elif sf == 73:
print("YPbPr(1125/60)")
elif sf == 74:
print("YPbPr(1250/50)")
elif sf == 75:
print("JPEG XR Codestream")
elif sf == 76:
print("JPEG XR Sub Baseline")
elif sf == 77:
print("JPEG XR Baseline")
elif sf == 78:
print("JPEG XR Main Profile")
elif sf == 79:
print("JPEG XR Advanced")
elif sf == 80:
print("Fix point data format")
elif sf == 81:
print("Floating point data format")
elif sf == 82:
print("Mantissa Exponent data format")
elif sf == 83:
print("scRGB color space")
else:
print("unknown standard flag %d " % sf)
box.print_indent(" Standard mask :",0)
for j in range(ml):
print("0x%02x " % ord(buffer[off]), end=' ')
off += 1
print()
nv = ordw(buffer[off:off+2])
off += 2
box.print_indent(" Number of vendor features : %d" % nv)
for i in range(nv):
box.print_indent(" Vendor feature UUID: ",0)
print("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" % \
(ord(buffer[off+ 0]), ord(buffer[off+ 1]), ord(buffer[off+ 2]), ord(buffer[off+ 3]),
ord(buffer[off+ 4]), ord(buffer[off+ 5]), ord(buffer[off+ 6]), ord(buffer[off+ 7]),
ord(buffer[off+ 8]), ord(buffer[off+ 9]), ord(buffer[off+10]), ord(buffer[off+11]),
ord(buffer[off+12]), ord(buffer[off+13]), ord(buffer[off+14]), ord(buffer[off+15])))
off += 16
box.print_indent(" Vendor mask : ",0)
for j in range(ml):
print("0x%02x " % ord(buffer[off]), end=' ')
off += 1
print()
def parse_uuid_box(box,buffer):
print("UUID box")
if len(buffer) < 16:
box.print_indent("invalid box")
return
box.print_indent("UUID :",0)
uuid = buffer[0:16]
if uuid == "\x2d\x41\x21\xde\xb0\xf1\x47\x43\x83\x5b\x00\xf4\x0b\xae\xc2\xed":
print("Pegasus J2K branding")
avbrand = 1
else:
print("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" % \
(uuid[ 0], uuid[ 1], uuid[ 2], uuid[ 3],
uuid[ 4], uuid[ 5], uuid[ 6], uuid[ 7],
uuid[ 8], uuid[ 9], uuid[10], uuid[11],
uuid[12], uuid[13], uuid[14], uuid[15]))
avbrand = 0
box.print_indent("UUID Data :")
if avbrand and len(buffer) >= 20:
box.print_indent("Pegasus Version: %d " % ordl(buffer[16:20]))
if len(buffer) > 20:
print("Additional Data:")
box.print_hex(buffer[20:])
else:
print()
box.print_hex(buffer[16:])
def parse_filetype_box(box,buffer):
box.print_indent("File Type box");
if len(buffer) < 8 or len(buffer) % 4 != 0:
box.print_indent("invalid box")
# Print BR (brand)
if buffer[0:4] == "jp2 ":
brand = "JP2"
elif buffer[0:4] == "jpxb":
brand = "JPX baseline"
elif buffer[0:4] == "jpx ":
brand = "JPX"
elif buffer[0:4] == "mjp2":
brand = "mjp2"
elif buffer[0:4] == "mjps":
brand = "mjp2"
elif buffer[0:4] == "jpxt":
brand = "JPEG XT"
elif buffer[0:4] == "jxs ":
brand = "JPEG XS"
else:
brand = "0x%02x%02x%02x%02x" % \
(buffer[0],buffer[1],buffer[2],buffer[3])
box.print_indent("Brand : %s" % brand)
# Print MinV (minor version)
minv = ordl(buffer[4:8])
box.print_indent("Minor version: %d" % (minv))
# Print CL (Compatibility List)
box.print_indent("Compatibility: ",0)
clsize = (len(buffer) - 8) / 4
for i in range(int(clsize)):
offset = i * 4 + 8
if buffer[offset:offset+4] == "jp2 ":
print("JPEG2000", end=' ')
elif buffer[offset:offset+4] == "J2P0":
print("JPEG2000,Profile 0", end=' ')
elif buffer[offset:offset+4] == "J2P1":
print("JPEG2000,Profile 1", end=' ')
elif buffer[offset:offset+4] == "jpxb":
print("JPEG2000-2,JPX", end=' ')
elif buffer[offset:offset+4] == "jpx ":
print("JPEG2000-2", end=' ')
elif buffer[offset:offset+4] == "mjp2":
print("Motion JPEG2000", end=' ')
elif buffer[offset:offset+4] == "mjps":
print("Motion JPEG2000,Simple profile", end=' ')
elif buffer[offset:offset+4] == "jpxt":
print("JPEG XT", end=' ')
elif buffer[offset:offset+4] == "jxs ":
print("JPEG XS", end=' ')
elif buffer[offset:offset+4] == "irfp":
print("JPEG XT Intermediate Range Coding", end=' ')
elif buffer[offset:offset+4] == "xrdd":
print("JPEG XT HDR Coding profile A", end=' ')
elif buffer[offset:offset+4] == "xrxd":
print("JPEG XT HDR Coding profile B", end=' ')
elif buffer[offset:offset+4] == "xrad":
print("JPEG XT HDR Coding profile C", end=' ')
elif buffer[offset:offset+4] == "xrrf":
print("JPEG XT HDR Coding profile D", end=' ')
elif buffer[offset:offset+4] == "lsfp":
print("JPEG XT Lossless coding", end=' ')
elif buffer[offset:offset+4] == "acfp":
print("JPEG XT alpha coding full profile", end=' ')
elif buffer[offset:offset+4] == "acbp":
print("JPEG XT alpha coding base profile", end=' ')
else:
print("0x%02x%02x%02x%02x" % \
(buffer[offset + 0],
buffer[offset + 1],
buffer[offset + 2],
buffer[offset + 3]), end=' ')
print()
def parse_image_header_box(box,buffer):
print("Image Header box")
if len(buffer) != 14:
box.print_indent ("invalid box")
return
box.print_indent("Height : %d" % ordl(buffer[0:4]))
box.print_indent("Width : %d" % ordl(buffer[4:8]))
box.print_indent("Components : %d" % ordw(buffer[8:10]))
box.print_indent("Bits Per Component : %d" %
((buffer[10] & 0x7f) + 1))
box.print_indent("Signed Components : ",0)
if buffer[10] & 0x80:
print("yes")
else:
print("no")
box.print_indent("Compression Type : ",0)
if buffer[11] == 0:
print("uncompressed")
elif buffer[11] == 1:
print("ITU T.4 / modified Huffman")
elif buffer[11] == 2:
print("ITU T.4 / modified READ")
elif buffer[11] == 3:
print("ITU T.6 / modified modified READ")
elif buffer[11] == 4:
print("JBIG")
elif buffer[11] == 5:
print("JPEG")
elif buffer[11] == 6:
print("JPEG-LS")
elif buffer[11] == 7:
print("JPEG 2000")
elif buffer[11] == 8:
print("JBIG2")
elif buffer[11] == 9:
print("JBIG")
elif buffer[11] == 11:
print("JPEG XR")
elif buffer[11] == 12:
print("JPEG XS")
else:
print("unknown (%d)" % buffer[11])
box.print_indent("Unknown Colourspace : ",0)
if buffer[12] == 0:
print("no")
elif buffer[12] == 1:
print("yes")
else:
print("invalid value")
box.print_indent("Intellectual Property: ",0)
if buffer[13] == 0:
print("no")
elif buffer[13] == 1:
print("yes")
else:
print("invalid value")
def parse_bpc_box(box,buffer):
print("Bits Per Component box")
bpc = len(buffer)
for i in range(bpc):
b = ord(buffer[i])
depth = (b & 0x7f) + 1
sign = b & 0x80
if sign:
sign = "yes"
else:
sign = "no"
box.print_indent("Bit Depth #%d: %d" % (i, depth))
box.print_indent("Signed #%d: %s" % (i, sign))
def parse_colorspec_box(box,buffer):
print("Colour Specification box")
if len(buffer) < 3:
box.print_indent("invalid box")
return
# Unfortunately, this thing comes in two variants, the
# jp2-inherited variant, and the iso-bmff variant.
# Urgh. Try a best-attempt to find out what it is.
id = buffer[0:4]
offset = 4
if id == "nclx":
method = 5
elif id == "rICC":
method = 2
elif id == "prof":
method = 3
else:
method = buffer[0]
offset = 3
box.print_indent("Colour Specification Method: ",0)
if method == 1:
print("enumerated colourspace")
elif method == 2:
print("restricted ICC profile")
elif method == 3:
print("full icc profile")
elif method == 4:
print("parametric colourspace")
elif method == 5:
print("coding independent code points")
else:
print("unknown")
if offset == 3:
prec = buffer[1]
if prec >= 128:
prec -= 256;
box.print_indent("Precedence : %d" % prec)
box.print_indent("Approximation: %d" % ord(buffer[2]))
if method == 1:
cs = ordl(buffer[offset:offset + 4])
if len(buffer) != 7 and cs != 19 and cs != 14:
box.print_indent("invalid box")
return
box.print_indent("Colourspace :",0)
if cs == 16:
print("sRGB")
elif cs == 17:
print("greyscale")
elif cs == 18:
print("YCC")
elif cs == 14:
print("CIELab")
if len(buffer) != 7+4*7:
box.print_indent("invalid box")
return
rl = ordl(buffer[offset + 4:offset + 8])
ol = ordl(buffer[offset + 8:offset + 12])
ra = ordl(buffer[offset + 12:offset + 16])
oa = ordl(buffer[offset + 16:offset + 20])
rb = ordl(buffer[offset + 20:offset + 24])
ob = ordl(buffer[offset + 24:offset + 28])
il = ordl(buffer[offset + 28:offset + 32])
box.print_indent("Range L : %d" % rl)
box.print_indent("Origin L : %d" % ol)
box.print_indent("Range a : %d" % ra)
box.print_indent("Origin a : %d" % oa)
box.print_indent("Range b : %d" % rb)
box.print_indent("Origin b : %d" % ob)
box.print_indent("Illuminant : %08x" % il)
elif cs == 19:
print("CIEJab")
if len(buffer) != 7+4*6:
box.print_indent("invalid box")
return
rj = ordl(buffer[offset + 4:offset + 8])
oj = ordl(buffer[offset + 8:offset + 12])
ra = ordl(buffer[offset + 12:offset + 16])
oa = ordl(buffer[offset + 16:offset + 20])
rb = ordl(buffer[offset + 20:offset + 24])
ob = ordl(buffer[offset + 24:offset + 28])
box.print_indent("Range J : %d" % rj)
box.print_indent("Origin J : %d" % oj)
box.print_indent("Range a : %d" % ra)
box.print_indent("Origin a : %d" % oa)
box.print_indent("Range b : %d" % rb)
box.print_indent("Origin b : %d" % ob)
elif cs == 20:
print("esRGB")
elif cs == 21:
print("rommRGB")
elif cs == 24:
print("esYCC")
elif cs == 25:
print("scRGB")
elif cs == 0:
print("black on white")
elif cs == 1:
print("YCbCr(1)")
elif cs == 3:
print("YCbCr(2)")
elif cs == 4:
print("YCbCr(3)")
elif cs == 9:
print("PhotoYCC")
elif cs == 11:
print("CMY")
elif cs == 12:
print("CMYK")
elif cs == 13:
print("YCCK")
elif cs == 15:
print("white on black")
elif cs == 22:
print("YPbPr(1125/60)")
elif cs == 23:
print("YPbPr(1250/50)")
else:
print("unknown (%d)" % (cs))
elif method == 2 or method == 3:
box.print_indent("ICC Colour Profile:")
parse_icc(box.indent,buffer[offset:])
#box.print_hex(buffer[3:])
elif method == 5:
cp = ordw(buffer[offset:offset + 2])
tc = ordw(buffer[offset + 2:offset + 4])
mc = ordw(buffer[offset + 4:offset + 6])
v = ord(buffer[offset + 6:offset + 7])
if cp == 1 and tc == 13 and mc == 0 and v == 0:
colorspec = "IEC 61966-2-1 sRGB"
elif cp == 1 and tc == 13 and mc == 1 and v == 0:
colorspec = "IEC 61966-2-1 sYCC"
elif cp == 1 and tc == 1 and mc == 1 and v == 0:
colorspec = "BT.709-6 full range"
elif cp == 1 and tc == 1 and mc == 1 and v == 128:
colorspec = "BT.709-6 with head & toe region"
elif cp == 5 and tc == 6 and mc == 5 and v == 0:
colorspec = "BT.601-7 625 full range"
elif cp == 5 and tc == 6 and mc == 5 and v == 128:
colorspec = "BT.601-7 625 with head & toe region"
elif cp == 6 and tc == 6 and mc == 6 and v == 0:
colorspec = "BT.601-7 525 full range"
elif cp == 6 and tc == 6 and mc == 6 and v == 128:
colorspec = "BT.601-7 525 with head & toe region"
elif cp == 9 and (tc == 14 or tc == 15) and (mc == 9 or mc == 10) and v == 0:
colorspec = "BT.2020-2 full range"
elif cp == 9 and (tc == 14 or tc == 15) and (mc == 9 or mc == 10) and v == 128:
colorspec = "BT.2020-2 with head & toe region"
elif cp == 9 and (tc == 16 or tc == 18) and mc == 9 and v == 0:
colorspec = "BT.2100-0 full range"
elif cp == 9 and (tc == 16 or tc == 18) and mc == 9 and v == 128:
colorspec = "BT.2100-0 with head & toe region"
elif cp == 10 and tc == 17 and mc == 0 and v == 0:
colorspec = "SMPTE ST 428-1"
elif cp == 11 and tc == 17 and mc == 0 and v == 0:
colorspec = "SMPTE RP 431-2"
elif cp == 12 and tc == 17 and mc == 0 and v == 0:
colorspec = "SMPTE EG 432-1"
else:
if v == 0:
vrange = "full range"
elif v == 128:
vrange = "with head & toe region"
else:
vrange = "invalid (%s)" % v
colorspec = "Primaries: %d, Transfer: %d, Matrix: %d %s" % (cp,tc,mc,vrange)
box.print_indent("Colour Space : %s" % colorspec)
else:
box.print_indent("Colour Data:")
box.print_hex(buffer[offset:])
def parse_palette_box(box, buffer):
print("Palette box")
if len(buffer) < 3:
box.print_indent("invalid box")
return
ne = ordw(buffer[0:2])
box.print_indent("Entries : %d" % ne)
npc = ord(buffer[2])
box.print_indent("Created Channels: %d" % npc)
# Read B[n] list
if len(buffer) - 3 < npc:
box.print_indent("invalid box")
return
depths = []
entrysizes = []
entrysize = 0 # byte size of one palette row
for i in range(npc):
b = ord(buffer[3 + i])
depth = (b & 0x7f) + 1
depths.append(depth)
if b & 0x80:
sign = "yes"
else:
sign = "no"
box.print_indent("Depth #%d : %d" % (i, depth))
box.print_indent("Signed #%d : %s" % (i, sign))
es = depth / 8
if depth % 8 != 0:
es = es + 1
entrysizes.append(es)
entrysize = entrysize + es
# Read C[n,m] list
if len(buffer) - 3 - npc != entrysize * ne:
box.print_indent("invalid box")
return
pos = 3 + npc
for i in range(ne):
box.print_indent("Entry #%03d:" % (i),0)
values = []
for j in range(npc):
v = 0
for k in range(entrysizes[j]):
v = v << 8
v = v + ord(buffer[pos])
pos = pos + 1
print("0x%010x" % (v), end=' ')
print()
def parse_cmap_box(box,buffer):
print("Component Mapping box")
if len(buffer) % 4 != 0:
box.print_indent("invalid box")
return
entries = len(buffer) / 4
for i in range(entries):
cmp = ordw(buffer[i * 4 + 0:i * 4 + 2])
mtyp = ord(buffer[i * 4 + 2])
pcol = ord(buffer[i * 4 + 3])
box.print_indent("Component #%d: %d" % (i, cmp))
box.print_indent("Mapping Type #%d:" % (i),0)
if mtyp == 0:
print("direct use")
elif mtyp == 1:
print("palette mapping")
else:
print("unknown")
box.print_indent("Palette Column #%d: %d" % (i,pcol))
def parse_opct_box(box,buffer):
print("Opacity box")
if len(buffer) < 1:
box.print_indent("invalid box")
return
typ = ord(buffer[0])
if typ == 0:
box.print_indent("Opacity Type : last channel is opacity channel")
elif typ == 1:
box.print_indent("Opacity Type : last channel is premultiplied opacity channel");
elif typ == 2:
if len(buffer) < 2:
box.print_indent("invalid box")
box.print_indent("Opacity Type : opacity by chroma key");
nch = buffer[1]
# The following is actually a bug. I need to know the bit depth
# to parse the opacity channel, but the bit depth is not in here.
for i in range(nch):
box.print_indent("Chroma key for channel #%d: 0x%02x" % (i,ord(buffer[2+i])))
def parse_cdef_box(box,buffer):
print("Channel Definition box")
if len(buffer) < 2:
box.print_indent("invalid box")
return
num = ordw(buffer[0:2])
if len(buffer) - 2 != num * 6:
box.print_indent("invalid box")
return
for i in range(num):
cn = ordw(buffer[i * 6 + 2:i * 6 + 4])
typ = ordw(buffer[i * 6 + 4:i * 6 + 6])
asoc = ordw(buffer[i * 6 + 6:i * 6 + 8])
box.print_indent("Channel #%d: %d" % (i, cn))
box.print_indent("Type #%d:" % (i),0)
if typ == 0:
print("color")
elif typ == 1:
print("opacity")
elif typ == 2:
print("premultiplied opacity")
elif typ == 3:
print("application defined color")
elif typ == 0xffff:
print("unspecified")
else:
print("unknown")
box.print_indent("Association #%d:" % (i),0)
if asoc == 0:
print("whole image")
elif asoc == 0xffff:
print("none")
else:
print("%x" % asoc)
def parse_label_box(box,buffer):
print("Label box")
box.print_indent("Content : %s" % buffer)
def parse_nlst_box(box,buffer):
box.print_indent("Number list box")
size = len(buffer)
if size < 4 or size % 4 != 0:
box.print_indent("invalid box")
return
size = size / 4
box.print_indent("Number of entries : %d" % size)
offset = 0
for i in range(size):
an = ordl(buffer[offset + 0:offset + 4])
if an == 0:
asoc = "the rendered result"
else:
atyp = an >> 24
aid = an & 0x00ffffff
if atyp == 1:
asoc = "codestream # %d" % aid
elif atyp == 2:
asoc = "compositing layer # %d" % aid
else:
asoc = "invalid association # %d" % aid
box.print_indent("Association to item : %s" % asoc)
offset += 4
def parse_copt_box(box,buffer):
box.print_indent("Composition options box")
if len(buffer) != 9:
box.print_indent("invaild box")
return
height = ordl(buffer[0:4])
width = ordl(buffer[4:8])
loop = ord(buffer[8])
box.print_indent("Rendered result width : %d" % width)
box.print_indent("Rendered result height : %d" % height)
box.print_indent("Looping count : %d" % loop)
def parse_inst_box(box,buffer):
box.print_indent("Instruct set box");
if len(buffer) < 8:
box.print_indent("invalid box")
return
ityp = ordw(buffer[0:2])
rept = ordw(buffer[2:4])
tick = ordl(buffer[4:8])
size = 0
if ityp & 1:
offsets = "yes"
size += 8
else:
offsets = "no"
if ityp & 2:
dimens = "yes"
size += 8
else:
dimens = "no"
if ityp & 4:
life = "yes"
size += 8
else:
life = "no"
if ityp & 32:
crop = "yes"
size += 16
else:
crop = "no"
box.print_indent("Layer offsets required : %s" % offsets)
box.print_indent("Layer scaling required : %s" % dimens)
box.print_indent("Life time and persistence included: %s" % life)
box.print_indent("Image cropping required : %s" % crop)
box.print_indent("Number of repetitions : %d" % rept)
box.print_indent("Duration of a timer tick : %dms" % tick)
offset = 8
length = len(buffer) - offset
if size != 0:
if length % size != 0:
box.print_indent("invalid box length")
return
entries = length / size
else:
if length != 0:
box.print_indent("invalid box length")
return
entries = 0
box.print_indent("Number of instructions : %d" % entries)
for i in range(entries):
box.print_indent("")
box.print_indent("Instruction # : %d" % (i+1))
if ityp & 1:
offx = ordl(buffer[offset + 0:offset + 4])
offy = ordl(buffer[offset + 4:offset + 8])
box.print_indent("Horizontal layer offset : %d" % offx)
box.print_indent("Vertical layer offset : %d" % offy)
offset += 8
if ityp & 2:
width = ordl(buffer[offset + 0:offset + 4])
height= ordl(buffer[offset + 4:offset + 8])
box.print_indent("Scaled layer width : %d" % width)
box.print_indent("Scaled layer height : %d" % height)
offset += 8
if ityp & 4:
if ord(buffer[offset]) & 0x80:
persist = "yes"
else:
persist = "no"
life = ordl(buffer[offset + 0:offset + 4]) & 0x7fffffff
use = ordl(buffer[offset + 4:offset + 8])
box.print_indent("Layer pixels shall persist : %s" % persist)
if life == (1 << 31) - 1:
box.print_indent("Layer life time : forever")
else:
box.print_indent("Layer life time : %d" % life)
box.print_indent("Number of instructions to resual : %d" % use)
offset += 8
if ityp & 32:
xc = ordl(buffer[offset + 0:offset + 4])
yc = ordl(buffer[offset + 4:offset + 8])
wc = ordl(buffer[offset + 8:offset + 12])
hc = ordl(buffer[offset + 12:offset + 16])
box.print_indent("Horizontal cropping offset : %d" % xc)
box.print_indent("Vertical cropping offset : %d" % yc)
box.print_indent("Cropped width : %d" % wc)
box.print_indent("Cropped height : %d" % hc)
offset += 16
def parse_creg_box(box,buffer):
box.print_indent("Codestream registration box")
if len(buffer) < 4:
box.print_indent("invalid box")
return
xs = ordw(buffer[0:2])
ys = ordw(buffer[2:4])
box.print_indent("Horizontal grid size : %d" % xs)
box.print_indent("Vertical grid size : %d" % ys)
offset = 4
length = len(buffer) - offset
if length % 6 != 0:
box.print_indent("invalid box length")
entries = length / 6
for i in range(entries):
cod = ordw(buffer[offset + 0:offset + 2])
xr = ord(buffer[offset + 2])
yr = ord(buffer[offset + 3])
xo = ord(buffer[offset + 4])
yo = ord(buffer[offset + 5])
box.print_indent("Codestream number : %d" % cod)
box.print_indent("Horizontal resolution : %d" % xr)
box.print_indent("Vertical resolution : %d" % yr)
box.print_indent("Horizontal offset : %d" % xo)
box.print_indent("Vertical offset : %d" % yo)
offset = offset + 6
def parse_flst_box(box,buffer):
box.print_indent("Fragment list box")
offset = 0
nf = (ord(buffer[offset]) << 8) + (ord(buffer[offset+1]))
offset += 2
for i in range(nf):
off = ordq(buffer[offset + 0:offset + 8])
ln = ordl(buffer[offset + 8:offset + 12])
dr = ordw(buffer[offset + 12:offset + 14])
box.print_indent("fragment start: %10d, size %10d, data xref %x" % (off,ln,dr))
offset += 14
def parse_cref_box(box,buffer):
print("Cross reference box")
if len(buffer) < 14:
box.print_indent("invalid box")
return
type = buffer[0:4]
box.print_indent("Referenced box: %s" % type)
size = buffer[4:8]
type = buffer[8:12]
if type == "flst":
box.new_box("\"%s\"" % type)
parse_flst_box(box,buffer[12:len(buffer)])
box.end_box
else:
box.print_indent("sub-box %s is not a fragment list box" % type)
def parse_signature_box(box,buffer):
print("JP2 Signature box")
box.print_indent("Corrupted:",0)
if buffer == "\x0d\x0a\x87\x0a":
print("no")
else:
print("yes")
def parse_xml_box(box,buffer):
print("XML box")
box.print_indent("Data:")
s = buffer
if s[len(s) - 1] == "\0":
s = s[:len(s) - 2]
box.print_indent(s.decode('utf-8'))
def parse_uuidlist_box(box,buffer):
print("UUID List box")
if len(buffer) < 2:
box.print_indent("invalid box")
return
ne = ordw(buffer[0:2])
if len(buffer) != ne * 16 + 2:
box.print_indent("invalid box")
return
for i in range(0, ne):
box.print_indent("UUID #%d: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" \
% (i, \
buffer[ 2], buffer[ 3], buffer[ 4],
buffer[ 5], buffer[ 6], buffer[ 7],
buffer[ 8], buffer[ 9], buffer[10],
buffer[11], buffer[12], buffer[13],
buffer[14], buffer[15], buffer[16],
buffer[17]))
buffer = buffer[16:]
def parse_url_box(box,buffer):
print("Data Entry URL box")
if len(buffer) < 5:
box.print_indent("invalid box")
return
box.print_indent("Version: %d" % ord(buffer[0]))
box.print_indent("Flags: 0x%02x%02x%02x" % \
(buffer[1], ord(buffer[2]), ord(buffer[3])))
box.print_indent("URL: %s" % fromCString(buffer[4:]))
def parse_roi_box(box,buffer):
print("ROI description box")
if len(buffer) < 1:
box.print_indent("invalid box")
count = ord(buffer[0])
box.print_indent("Number of ROIs : %d" % count)
offset = 1