-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsonparser
executable file
·1147 lines (889 loc) · 30.6 KB
/
jsonparser
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
# Copyright 2014 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# TODO(chromium:395947): Share strings and options with jsonedid
# TODO(chromium:395947): Add JSON validation
"""Creates an EDID binary blob out of a Json representation of an EDID."""
from __future__ import print_function
import itertools
import json
import sys
import edid.edid as edid_module
import options as options_module
def _BuildBitsFromOptions(options, json_map):
"""Encodes a list of options into bit form for an EDID binary blob.
The order of the options determines the bit position in the EDID. The first
option corresponds to the most significant bit in the result, the last option
with the least significant bit, etc.
Args:
options: The list of options (strings).
json_map: The json dictionary indicating whether each option is true or
false (i.e., supported or not).
Returns:
An integer to be stored in the EDID that encodes these options.
"""
bits = 0
for option in options:
bits = (bits << 1) + int(json_map[option])
return bits
def BuildManufacturerInfo(edid, manu_json):
"""Adds information from manufacturer info dictionary into the EDID list.
Args:
edid: The full list form of the EDID.
manu_json: The dictionary of manufacturer info.
"""
# Manufacturer Name
manu_id = manu_json['Manufacturer ID']
m1 = (ord(manu_id[0]) - 64) << 10
m2 = (ord(manu_id[1]) - 64) << 5
m3 = ord(manu_id[2]) - 64
m = m1 + m2 + m3
edid[0x08] = m >> 8
edid[0x09] = m & 0xFF
# ID Product Code
prod_code = manu_json['ID Product Code']
edid[0x0A] = prod_code & 0xFF
edid[0x0B] = prod_code >> 8
# Serial Number
ser_num = manu_json['Serial number']
if ser_num:
edid[0x0C] = ser_num & 0xFF
edid[0x0D] = (ser_num >> 8) & 0xFF
edid[0x0E] = (ser_num >> 16) & 0xFF
edid[0x0F] = (ser_num >> 24) & 0xFF
else:
edid[0x0C:0x10] = [0x00] * 4
# Manufacturer week and year
if manu_json['Week of manufacture']:
edid[0x10] = manu_json['Week of manufacture']
edid[0x11] = manu_json['Year of manufacture'] - 1990
else:
if manu_json['Year of manufacture']:
edid[0x10] = 0x00
edid[0x11] = manu_json['Year of manufacture'] - 1990
else:
edid[0x10] = 0xFF
edid[0x11] = manu_json['Model year'] - 1990
def BuildBasicDisplay(edid, bd_json):
"""Adds information from basic display info dictionary into the EDID list.
Args:
edid: The full list form of the EDID.
bd_json: The dictionary of basic display info.
"""
if bd_json['Video input type'] == 'Digital':
vid_input = 1
if not bd_json['Color Bit Depth']:
cbd = 0x00
elif 'Reserved' in bd_json['Color Bit Depth']:
cbd = 0x07
else:
x, _ = bd_json['Color Bit Depth'].split(' Bits')
cbd = int(x) / 2 - 2
dig_supp = {
None: 0x00,
'DVI': 0x01,
'HDMI-a': 0x02,
'HDMI-b': 0x03,
'MDDI': 0x04,
'DisplayPort': 0x05
}
supp = dig_supp[bd_json['Digital Video Interface Standard Support']]
edid[0x14] = (vid_input << 7) + (cbd << 4) + supp
else:
vid_input = 0
sig_supp = {
'+0.7/-0.3 V': 0x00,
'+0.714/-0.286 V': 0x01,
'+1.0/-0.4 V': 0x02,
'+0.7/0 V': 0x03
}
sig = sig_supp[bd_json['Video white and sync levels']]
vid_settings = [
'Blank-to-black setup expected',
'Separate sync supported',
'Composite sync (on HSync) supported',
'Sync on green supported',
'VSync serrated when composite/sync-on-green used'
]
sum_bits = _BuildBitsFromOptions(vid_settings, bd_json)
edid[0x14] = (vid_input << 7) + (sig << 5) + sum_bits
# Aspect Ratios or Maximum Dimensions
arl = bd_json['Aspect ratio (landscape)']
arp = bd_json['Aspect ratio (portrait)']
md = bd_json['Maximum dimensions (cm)']
if arl:
a, b = arl.split(' : ')
edid[0x15] = int((float(a) * 100.0) - 99)
if arp:
a, b = arp.split(' : ')
edid[0x16] = int((100.0 / float(a)) - 99)
if md:
edid[0x15] = md['x']
edid[0x16] = md['y']
# Gamma
g = bd_json['Display gamma']
edid[0x17] = int((g * 100.0) - 100)
# Feature Support
dpm = [
'DPM standby supported',
'DPM suspend supported',
'DPM active-off supported'
]
sum_dpm = _BuildBitsFromOptions(dpm, bd_json)
sce = bd_json['Display color type']
if bd_json['Video input type'] == 'Digital':
a = 1 if 'YCrCb 4:2:2' in sce else 0
b = 1 if 'YCrCb 4:4:4' in sce else 0
color = (a << 1) + b
else:
color_types = {
'Monochrome/Grayscale': 0x00,
'RGB color': 0x01,
'Non-RGB color': 0x02,
'Undefined': 0x03
}
color = color_types[sce]
fsf = [
'sRGB Standard is default colour space',
'Preferred timing includes native timing pixel format and refresh rate',
'Continuous frequency supported'
]
sum_fsf = _BuildBitsFromOptions(fsf, bd_json)
edid[0x18] = (sum_dpm << 5) + (color << 3) + sum_fsf
def BuildChromaticity(edid, chrom_json):
"""Adds information from chromaticity info dictionary into the EDID list.
Args:
edid: The full list form of the EDID.
chrom_json: The dictionary of chromaticity info.
"""
rx = chrom_json['Red']['x'] & 0x03
ry = chrom_json['Red']['y'] & 0x03
gx = chrom_json['Green']['x'] & 0x03
gy = chrom_json['Green']['y'] & 0x03
bx = chrom_json['Blue']['x'] & 0x03
by = chrom_json['Blue']['y'] & 0x03
wx = chrom_json['White']['x'] & 0x03
wy = chrom_json['White']['y'] & 0x03
edid[0x19] = (rx << 6) + (ry << 4) + (gx << 2) + gy
edid[0x1A] = (bx << 6) + (by << 4) + (wx << 2) + wy
edid[0x1B] = chrom_json['Red']['x'] >> 2
edid[0x1C] = chrom_json['Red']['y'] >> 2
edid[0x1D] = chrom_json['Green']['x'] >> 2
edid[0x1E] = chrom_json['Green']['y'] >> 2
edid[0x1F] = chrom_json['Blue']['x'] >> 2
edid[0x20] = chrom_json['Blue']['y'] >> 2
edid[0x21] = chrom_json['White']['x'] >> 2
edid[0x22] = chrom_json['White']['y'] >> 2
def BuildEstablishedTimings(edid, et_json):
"""Adds information from established timings info dictionary into EDID list.
Args:
edid: The full list form of the EDID.
et_json: The dictionary of established timings info.
"""
sum_bits = _BuildBitsFromOptions(options_module.timings, et_json)
edid[0x23] = sum_bits >> 16
edid[0x24] = (sum_bits >> 8) & 0xFF
edid[0x25] = sum_bits & 0xFF
def BuildStandardTimings(edid, sts_json):
"""Adds information from standard timings info dictionary into the EDID list.
Args:
edid: The full list form of the EDID.
sts_json: The list of dictionaries of standard timings info.
"""
# First, set the standard timings that are defined
num_st = len(sts_json)
base = 0x26
edid[base:(base + 16)] = [0x01] * 16
for x in xrange(0, num_st):
edid[base + (x * 2):base + 2 + (x * 2)] = BuildSt(sts_json[x])
def BuildSt(one_st_json):
"""Creates a list out of a single standard timing object's dictionary.
Args:
one_st_json: The dictionary of a single standard timing object info.
Returns:
A list of two bytes representing a single standard timing object.
"""
x = (one_st_json['X resolution'] / 8) - 31
ratios = {
'1:1': 0x00,
'16:10': 0x00,
'4:3': 0x01,
'5:4': 0x02,
'16:9': 0x03
}
iar = ratios[one_st_json['Ratio']]
frr = one_st_json['Frequency'] - 60
return [x, (iar << 6) + frr]
def BuildDescriptors(edid, descs_json):
"""Adds information from descriptors info dictionary into the EDID list.
Args:
edid: The full list form of the EDID.
descs_json: The list of dictionaries of descriptor info.
"""
base = 0x36
for x in xrange(0, 4):
edid[base + (x * 18):base + ((x + 1) * 18)] = BuildDescriptor(descs_json[x])
def BuildDtd(desc_json):
"""Creates a list out of a single detailed timing descriptor dictionary.
Args:
desc_json: The dictionary of a single detailed timing descriptor info.
Returns:
A list of 18 bytes representing a single detailed timing descriptor.
"""
d = [0] * 18
# Set pixel clock
pc = int(desc_json['Pixel clock (MHz)'] * 100)
d[0] = pc & 0xFF
d[1] = pc >> 8
hav = desc_json['Addressable']['x']
hb = desc_json['Blanking']['x']
d[2] = hav & 0xFF
d[3] = hb & 0xFF
d[4] = ((hav >> 8) << 4) + (hb >> 8)
vav = desc_json['Addressable']['y']
hv = desc_json['Blanking']['y']
d[5] = vav & 0xFF
d[6] = hv & 0xFF
d[7] = ((vav >> 8) << 4) + (hv >> 8)
hfp = desc_json['Front porch']['x']
hsp = desc_json['Sync pulse']['x']
vfp = desc_json['Front porch']['y']
vsp = desc_json['Sync pulse']['y']
d[8] = hfp & 0xFF
d[9] = hsp & 0xFF
d[10] = ((vfp & 0x0F) << 4) + (vsp & 0x0F)
d[11] = ((hfp >> 8) << 6) + ((hsp >> 8) << 4) + ((vfp >> 4) << 2) + (vsp >> 4)
hav = desc_json['Image size (mm)']['x']
vav = desc_json['Image size (mm)']['y']
d[12] = hav & 0xFF
d[13] = vav & 0xFF
d[14] = ((hav >> 8) << 4) + (vav >> 8)
d[15] = desc_json['Border']['x']
d[16] = desc_json['Border']['y']
# Byte 17
stereo_map = {
'No stereo': (0x0 << 5) + 0x0, # Could be 0x00 or 0x01
'Field sequential stereo, right image when stereo sync signal = 1':
(0x1 << 5) + 0x0,
'2-way interleaved stereo, right image on even lines': (0x1 << 5) + 0x1,
'Field sequential stereo, left image when stereo sync signal = 1':
(0x2 << 5) + 0x0,
'2-way interleaved stereo, left image on even lines': (0x2 << 5) + 0x1,
'4-way interleaved stereo': (0x3 << 5) + 0x0,
'Side-by-side interleaved stereo': (0x3 << 5) + 0x1
}
stereo = stereo_map[desc_json['Stereo viewing']]
sync_json = desc_json['Sync type']
if 'Digital' in sync_json['Type']:
if 'Separate' in sync_json['Type']:
x = 0x03
y = 1 if sync_json['Vertical sync'] == 'Positive' else 0
else:
x = 0x02
y = 1 if sync_json['Serrations'] else 0
z = 1 if (sync_json['Horizontal sync (outside of V-sync)'] ==
'Positive') else 0
else: # Analog
x = 0x01 if 'Bipolar' in sync_json['Type'] else 0x00
y = 1 if sync_json['Serrations'] else 0
z = 1 if sync_json['Sync on RGB'] else 0
interlace = int(desc_json['Interlace'])
sync_type = (x << 3) + (y << 2) + (z << 1)
d[17] = (interlace << 7) + stereo + sync_type
return d
def BuildDescriptor(desc_json):
"""Creates a list out of a single descriptor object's dictionary.
Args:
desc_json: The dictionary of a single descriptor object info.
Returns:
A list of 18 bytes representing a single descriptor object.
"""
d = [0] * 18
atype = desc_json['Type']
if atype == 'Detailed Timing Descriptor':
return BuildDtd(desc_json)
else:
d[0] = d[1] = d[2] = 0x00
types = {
'Display Product Serial Number': 0xFF,
'Alphanumeric Data String (ASCII)': 0xFE,
'Display Range Limits Descriptor': 0xFD,
'Display Product Name': 0xFC,
'Color Point Data': 0xFB,
'Standard Timing Identifiers': 0xFA,
'Display Color Management (DCM) Data': 0xF9,
'CVT 3 Byte Timing Codes': 0xF8,
'Established Timings III': 0xF7,
'Error: Reserved/undefined; do not use': 0x11,
'Dummy descriptor': 0x10,
'Manufacturer Specified Display Descriptor': 0x00, # 0x00 to 0xF6
}
d[3] = types[atype]
if d[3] in [0xFF, 0xFE, 0xFC]: # Type of string descriptor
d[5:18] = [0x20] * 13 # Padding with 0x20
data = desc_json['Data string']
str_len = len(data)
d[5:(5 + str_len)] = map(ord, data)
if str_len < 13:
d[5 + str_len] = 0x0A
elif atype == 'Display Range Limits Descriptor':
subtypes = {
'Default GTF supported': 0x00,
'Range Limits Only - no additional info': 0x01,
'Secondary GTF supported - requires default too': 0x02,
'CVT supported': 0x04,
'Unknown': 0x03 # Could be 0x03, 0x05+
}
asubtype = desc_json['Subtype']
d[10] = subtypes[asubtype]
vmin = int(desc_json['Vertical rate (Hz)']['Minimum'])
vmax = int(desc_json['Vertical rate (Hz)']['Maximum'])
hmin = int(desc_json['Horizontal rate (kHz)']['Minimum'])
hmax = int(desc_json['Horizontal rate (kHz)']['Maximum'])
h = hmax / 256
i = hmin / 256
j = vmax / 256
k = vmin / 256
d[4] = (h << 3) + (i << 2) + (j << 1) + k
d[5] = vmin % 256
d[6] = vmax % 256
d[7] = hmin % 256
d[8] = hmax % 256
d[9] = int(desc_json['Pixel clock (MHz)'] / 10)
if asubtype == 'Secondary GTF supported - requires default too':
d[12] = desc_json['Start break frequency'] / 2
d[13] = desc_json['C'] * 2
d[14] = desc_json['M'] & 0xFF
d[15] = desc_json['M'] >> 8
d[16] = desc_json['K']
d[17] = desc_json['J'] * 2
elif asubtype == 'CVT supported':
v, r = desc_json['CVT Version'].split('.')
d[11] = (int(v) << 4) + int(r)
apc = int(desc_json['Additional Pixel Clock (MHz)'] / 0.25)
maxap = desc_json['Maximum active pixels']
maxap = int(maxap) / 8 if maxap else 0
d[12] = (apc << 2) + (maxap >> 8)
d[13] = maxap & 0xFF
ratios = [
'4:3 AR',
'16:9 AR',
'16:10 AR',
'5:4 AR',
'15:9 AR'
]
d[14] = _BuildBitsFromOptions(ratios,
desc_json['Supported aspect ratios']) << 3
par = ratios.index(desc_json['Preferred aspect ratio'])
cvt_blank = desc_json['CVT blanking support']
rcvt = 1 if cvt_blank['Reduced CVT Blanking'] else 0
scvt = 1 if cvt_blank['Standard CVT Blanking'] else 0
d[15] = (par << 5) + (rcvt << 4) + (scvt << 3)
scalings = [
'Horizontal Shrink',
'Horizontal Stretch',
'Vertical Shrink',
'Vertical Stretch'
]
d[16] = _BuildBitsFromOptions(scalings,
desc_json['Display scaling support']) << 4
d[17] = desc_json['Preferred vertical refresh (Hz)']
else: # Not Secondary GTF or CVT supported
d[11] = 0x0A
d[12:18] = [0x20] * 6
elif atype == 'Manufacturer Specified Display Descriptor':
d[5:18] = desc_json['Blob']
elif atype == 'Established Timings III':
est_timings = [
[0x80000000000, '640 x 350 @ 85 Hz'],
[0x40000000000, '640 x 400 @ 85 Hz'],
[0x20000000000, '720 x 400 @ 85 Hz'],
[0x10000000000, '640 x 480 @ 85 Hz'],
[0x8000000000, '848 x 480 @ 60 Hz'],
[0x4000000000, '800 x 600 @ 85 Hz'],
[0x2000000000, '1024 x 768 @ 85 Hz'],
[0x1000000000, '1152 x 864 @ 75 Hz'],
[0x800000000, '1280 x 768 @ 60 Hz (RB)'],
[0x400000000, '1280 x 768 @ 60 Hz'],
[0x200000000, '1280 x 768 @ 75 Hz'],
[0x100000000, '1280 x 768 @ 85 Hz'],
[0x80000000, '1280 x 960 @ 60 Hz'],
[0x40000000, '1280 x 960 @ 85 Hz'],
[0x20000000, '1280 x 1024 @ 60 Hz'],
[0x10000000, '1280 x 1024 @ 85 Hz'],
[0x8000000, '1360 x 768 @ 60 Hz'],
[0x4000000, '1440 x 900 @ 60 Hz (RB)'],
[0x2000000, '1440 x 900 @ 60 Hz'],
[0x1000000, '1440 x 900 @ 75 Hz'],
[0x800000, '1440 x 900 @ 85 Hz'],
[0x400000, '1400 x 1050 @ 60 Hz (RB)'],
[0x200000, '1400 x 1050 @ 60 Hz'],
[0x100000, '1400 x 1050 @ 75 Hz'],
[0x80000, '1400 x 1050 @ 85 Hz'],
[0x40000, '1680 x 1050 @ 60 Hz (RB)'],
[0x20000, '1680 x 1050 @ 60 Hz'],
[0x10000, '1680 x 1050 @ 75 Hz'],
[0x8000, '1680 x 1050 @ 85 Hz'],
[0x4000, '1600 x 1200 @ 60 Hz'],
[0x2000, '1600 x 1200 @ 65 Hz'],
[0x1000, '1600 x 1200 @ 70 Hz'],
[0x800, '1600 x 1200 @ 75 Hz'],
[0x400, '1600 x 1200 @ 85 Hz'],
[0x200, '1792 x 1344 @ 60 Hz'],
[0x100, '1792 x 1344 @ 75 Hz'],
[0x80, '1856 x 1392 @ 60 Hz'],
[0x40, '1856 x 1392 @ 75 Hz'],
[0x20, '1920 x 1200 @ 60 Hz (RB)'],
[0x10, '1920 x 1200 @ 60 Hz'],
[0x8, '1920 x 1200 @ 75 Hz'],
[0x4, '1920 x 1200 @ 85 Hz'],
[0x2, '1920 x 1440 @ 60 Hz'],
[0x1, '1920 x 1440 @ 75 Hz']
]
sum_bits = 0
for x, s in est_timings:
if desc_json['Established Timings'][s]:
sum_bits += x
sum_bits <<= 4
d[6] = (sum_bits >> 40)
d[7] = (sum_bits >> 32) & 0xFF
d[8] = (sum_bits >> 24) & 0xFF
d[9] = (sum_bits >> 16) & 0xFF
d[10] = (sum_bits >> 8) & 0xFF
d[11] = sum_bits & 0xFF
elif atype == 'Color Point Data':
cps = desc_json['Color Points'] # List of dicts
start = 5
for cp in cps:
d[start] = cp['Index number']
wx = cp['White point coordinates']['x']
wy = cp['White point coordinates']['y']
d[start + 1] = ((wx & 0x03) << 2) + (wy & 0x03)
d[start + 2] = wx >> 2
d[start + 3] = wy >> 2
gamma = cp['Gamma']
d[start + 4] = int((gamma * 100) - 100) if gamma else 0xFF
start += 5
# If there's only one color point, the 2nd remains all 0x00
elif atype == 'Standard Timing Identifiers':
sts = desc_json['Standard Timings']
for x in xrange(0, 6):
d[5 + (x * 2):7 + (x * 2)] = BuildSt(sts[x])
d[17] = 0x0A
elif atype == 'Display Color Management (DCM) Data':
d[5] = 0x03
d[6] = desc_json['Red a3'] & 0xFF
d[7] = desc_json['Red a3'] >> 8
d[8] = desc_json['Red a2'] & 0xFF
d[9] = desc_json['Red a2'] >> 8
d[10] = desc_json['Green a3'] & 0xFF
d[11] = desc_json['Green a3'] >> 8
d[12] = desc_json['Green a2'] & 0xFF
d[13] = desc_json['Green a2'] >> 8
d[14] = desc_json['Blue a3'] & 0xFF
d[15] = desc_json['Blue a3'] >> 8
d[16] = desc_json['Blue a2'] & 0xFF
d[17] = desc_json['Blue a2'] >> 8
elif atype == 'CVT 3 Byte Timing Codes':
d[5] = 0x01
cvts = desc_json['Coordinated Video Timings']
for x in xrange(0, len(cvts)): # Up to 4
d[6 + (x * 3):9 + (x * 3)] = BuildCvt(cvts[x])
return d
def BuildCvt(cvt_json):
"""Creates a list out of a single CVT object's dictionary.
Args:
cvt_json: The dictionary of a single CVT object info.
Returns:
A list of bytes representing a single CVT object.
"""
edid = [0] * 3
avl = (cvt_json['Active vertical lines'] / 2) - 1
edid[0] = avl & 0xFF
ratios = {
'4:3 AR': 0x00,
'16:9 AR': 0x01,
'16:10 AR': 0x02,
'15:9 AR': 0x03
}
ar = ratios[cvt_json['Aspect ratio']]
edid[1] = ((avl >> 4) & 0xF0) + (ar << 2)
vert_rate = {
'50Hz': 0x00,
'60Hz': 0x01,
'60Hz (reduced blanking)': 0x01,
'75Hz': 0x02,
'85Hz': 0x03
}
pref_vert = vert_rate[cvt_json['Preferred refresh rate']]
edid[2] = pref_vert << 5
rates = [
'50Hz',
'60Hz',
'75Hz',
'85Hz',
'60Hz (reduced blanking)'
]
edid[2] += _BuildBitsFromOptions(rates, cvt_json['Supported refresh rates'])
return edid
def BuildExtensions(edid, exts_json):
"""Adds information from extensions dictionary into the EDID list.
Args:
edid: The full list form of the EDID.
exts_json: The dictionary of extensions info.
"""
base = 0x80
for ext_json in exts_json:
edid[base:(base + 128)] = BuildExtension(ext_json)
base += 128
def BuildExtension(ext_json):
"""Creates a list out of a single extension object's dictionary.
Args:
ext_json: The dictionary of a single extension object info.
Returns:
A list of bytes representing a single extension object.
"""
e = [0] * 128
atype = ext_json['Type']
if atype == 'Video Timing Block Extension (VTB-EXT)':
e[0] = 0x10
e[1] = ext_json['Version']
dtds = ext_json['Detailed Timing Descriptors']
cvts = ext_json['Coordinated Video Timings']
sts = ext_json['Standard Timings']
e[2] = len(dtds)
e[3] = len(cvts)
e[4] = len(sts)
start = 5
for dtd in dtds:
e[start:(start + 0x12)] = BuildDtd(dtd)
start += 0x12
for cvt in cvts:
e[start:(start + 0x03)] = BuildCvt(cvt)
start += 0x03
for st in sts:
e[start:(start + 0x02)] = BuildSt(st)
start += 0x02
elif atype == 'CEA-861 Series Timing Extension':
e[0] = 0x02
e[1] = ext_json['Version']
supports = [
'Underscan',
'Basic audio',
'YCbCr 4:4:4',
'YCbCr 4:2:2'
]
e[3] = ((_BuildBitsFromOptions(supports, ext_json) << 4) +
ext_json['Native DTD count'])
index = 0x04
for db in ext_json['Data blocks']:
blob = BuildDataBlock(db)
length = len(blob)
e[index:(index + length)] = blob
index += length
e[2] = index # Where the DTDs start
for dtd in ext_json['Descriptors']:
e[index:(index + 18)] = BuildDtd(dtd)
index += 18
elif atype == 'Extension Block Map':
tags = ext_json['Tags']
e[1:(1 + len(tags))] = tags
return e
def BuildDataBlock(db_json):
"""Creates a list out of a single data block object's dictionary.
Args:
db_json: The dictionary of a single data block object info.
Returns:
A list of bytes representing a single data block object.
"""
atype = db_json['Type']
if atype == 'Audio Data Block':
tag = 0x01
extended_tag = None
sads = db_json['Short audio descriptors']
blob = list(itertools.chain(*[BuildSad(sad) for sad in sads]))
elif 'Video Data Block' in atype: # Regular Video Data Block or YCbCr 4:2:0
if atype == 'Video Data Block':
tag = 0x02
extended_tag = None
else: # YCbCr 4:2:0
tag = 0x07
extended_tag = 0x01
blob = [BuildSvd(svd) for svd in db_json['Short video descriptors']]
elif 'Vendor-Specific' in atype:
if atype == 'Vendor-Specific Data Block':
tag = 0x03
extended_tag = None
else:
tag = 0x07
extended_tag = 0x01 if 'Video' in atype else 0x17 # Audio
x, y, z = db_json['IEEE OUI'].split('-')
blob = [int(z, 16), int(y, 16), int(x, 16)] + db_json['Data payload']
elif atype == 'Speaker Allocation Block':
tag = 0x04
extended_tag = None
speakers = [
'Front Center High',
'Top Center',
'Front Left High / Front Right High',
'Front Left Wide / Front Right Wide',
'Rear Left Center / Rear Right Center',
'Front Left Center / Front Right Center',
'Rear Center',
'Rear Left / Rear Right',
'Front Center',
'LFE',
'Front Left / Front Right'
]
speaker_bits = _BuildBitsFromOptions(speakers,
db_json['Speaker allocation'])
blob = [speaker_bits & 0xFF, speaker_bits >> 8, 0]
elif atype == 'Colorimetry Data Block':
tag = 0x07
extended_tag = 0x05
colors = [
'Standard Definition Colorimetry based on IEC 61966-2-4',
'High Definition Colorimetry based on IEC 61966-2-4',
'Colorimetry based on IEC 61966-2-1/Amendment 1',
'Colorimetry based on IEC 61966-2-5, Annex A',
'Colorimetry based on IEC 61966-2-5',
'Colorimetry based on ITU-R BT.2020 YcCbcCrc',
'Colorimetry based on ITU-R BT.2020 YCbCr',
'Colorimetry based on ITU-R BT.2020 RGB'
]
blob = [_BuildBitsFromOptions(colors, db_json['Colorimetry']),
db_json['Metadata']]
elif atype == 'Video Capability Data Block':
tag = 0x07
extended_tag = 0x00
qy = 1 if db_json['YCC Quantization range'] else 0
qs = 1 if db_json['RGB Quantization range'] else 0
ou = {
'Undefined': 0x00,
'Not supported': 0x00,
'Overscan': 0x01,
'Underscan': 0x02,
'Both': 0x03
}
pt = ou[db_json['PT behavior']]
it = ou[db_json['IT behavior']]
ce = ou[db_json['CE behavior']]
blob = [(qy << 7) + (qs << 6) + (pt << 4) + (it << 2) + ce]
elif atype == 'InfoFrame Data Block':
tag = 0x07
extended_tag = 0x32
if_proc = db_json['InfoFrame Processing Descriptor']
if_pro_payload = if_proc['Data payload']
vsifs = db_json['Vendor-Specific Info Frames']
blob = [len(if_proc_payload) << 5, len(vsifs)] + if_proc_payload
for vsif in vsifs:
blob += BuildVsif(vsif)
elif atype == 'YCbCr 4:2:0 Capability Map Data Block':
tag = 0x07
extended_tag = 0x15
indices = db_json['Supported descriptor indices']
bit_map = 0
for index in indices:
bit_map |= (1 << index)
blob = []
while bit_map:
blob.append(bit_map & 0xFF)
bit_map >>= 8
elif atype == 'Video Format Preference Data Block':
tag = 0x07
extended_tag = 0x13
prefs = db['Video preferences']
blob = []
for pref in prefs:
if pref['Type'] == 'Video Preference VIC':
blob.append(pref['VIC'])
elif pref['Type'] == 'Video Preference DTD':
blob.append(pref['DTD index'] + 128)
else: # Reserved
blob.append(pref['SVR'])
length = len(blob) if not extended_tag else len(blob) + 1
header = [(tag << 5) + length]
if extended_tag:
header.append(extended_tag)
return header + blob
def BuildVsif(vsif_json):
"""Creates a list out of a single VSIF object's dictionary.
Args:
vsif_json: The dictionary of a single VSIF object info.
Returns:
A list of bytes representing a single VSIF object.
"""
codes = {
'Vendor Specific': 0x01,
'Auxiliary Video Information': 0x02,
'Source Product Description': 0x03,
'Audio': 0x04,
'MPEG Source': 0x05,
'NTSC VBI': 0x06,
'Unknown': 0x07
}
vtype = vsif_json['Type']
payload = vsif_json['Data payload']
header = [(len(payload) << 5) + codes[vtype]]
oui = []
if vtype == 'Vendor Specific':
x, y, z = vsif_json['IEEE OUI'].split('-')
oui = [int(z, 16), int(y, 16), int(x, 16)]
return header + oui + payload
def BuildSad(sad_json):
"""Creates a list out of a single SAD object's dictionary.
Args:
sad_json: The dictionary of a single SAD object info.
Returns:
A list of 3 bytes representing a single SAD object.
"""
sad = [0] * 3
sad_types = [
'Linear Pulse Code Modulation (LPCM)',
'AC-3',
'MPEG1 (Layers 1 and 2)',
'MP3 (MPEG1 Layer 3)',
'MPEG2 (multichannel)',
'AAC',
'DTS',
'ATRAC',
'One-bit audio (aka SACD)',
'E-AC-3',
'DTS-HD',
'MAT MLP/Dolby TrueHD',
'DST Audio',
'Microsoft WMA Pro',
'MPEG-4 HE AAC',
'MPEG-4 HE AAC v2',
'MPEG-4 AAC LC',
'DRA',
'MPEG-4 HE AAC + MPEG Surround',
'MPEG-4AAC LC + MPEG Surround',
'Unknown' ### Necessary?
]
tag = sad_types.index(sad_json['Type']) + 1
mcc = sad_json['Max channel count'] -1
sad[0] = (tag << 3) + mcc
freqs = [
'192kHz',
'176.4kHz',
'96kHz',
'88.2kHz',
'48kHz',
'44.1kHz',
'32kHz'
]
sad[1] = _BuildBitsFromOptions(freqs, sad_json['Supported sampling'])
if sad_json['Type'] == 'Linear Pulse Code Modulation (LPCM)':
bits = [
'24 bit',