]>
Commit | Line | Data |
---|---|---|
91b2e171 | 1 | ## |
50bd5d25 | 2 | ## This file is part of the libsigrokdecode project. |
91b2e171 BV |
3 | ## |
4 | ## Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | ## | |
6 | ## This program is free software; you can redistribute it and/or modify | |
7 | ## it under the terms of the GNU General Public License as published by | |
8 | ## the Free Software Foundation; either version 3 of the License, or | |
9 | ## (at your option) any later version. | |
10 | ## | |
11 | ## This program is distributed in the hope that it will be useful, | |
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ## GNU General Public License for more details. | |
15 | ## | |
16 | ## You should have received a copy of the GNU General Public License | |
17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | ## | |
19 | ||
91b2e171 BV |
20 | # TODO: |
21 | # - EDID < 1.3 | |
1063646c | 22 | # - add short annotations |
91b2e171 BV |
23 | # - Signal level standard field in basic display parameters block |
24 | # - Additional color point descriptors | |
25 | # - Additional standard timing descriptors | |
26 | # - Extensions | |
27 | ||
28 | import sigrokdecode as srd | |
29 | import os | |
30 | ||
31 | EDID_HEADER = [0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00] | |
32 | OFF_VENDOR = 8 | |
33 | OFF_VERSION = 18 | |
34 | OFF_BASIC = 20 | |
35 | OFF_CHROM = 25 | |
36 | OFF_EST_TIMING = 35 | |
37 | OFF_STD_TIMING = 38 | |
38 | OFF_DET_TIMING = 54 | |
39 | OFF_NUM_EXT = 126 | |
40 | OFF_CHECKSUM = 127 | |
41 | ||
91b2e171 BV |
42 | # Pre-EDID established timing modes |
43 | est_modes = [ | |
09059016 UH |
44 | '720x400@70Hz', |
45 | '720x400@88Hz', | |
46 | '640x480@60Hz', | |
47 | '640x480@67Hz', | |
48 | '640x480@72Hz', | |
49 | '640x480@75Hz', | |
50 | '800x600@56Hz', | |
51 | '800x600@60Hz', | |
52 | '800x600@72Hz', | |
53 | '800x600@75Hz', | |
54 | '832x624@75Hz', | |
55 | '1024x768@87Hz(i)', | |
56 | '1024x768@60Hz', | |
57 | '1024x768@70Hz', | |
58 | '1024x768@75Hz', | |
59 | '1280x1024@75Hz', | |
60 | '1152x870@75Hz', | |
91b2e171 BV |
61 | ] |
62 | ||
63 | # X:Y display aspect ratios, as used in standard timing modes | |
64 | xy_ratio = [ | |
65 | (16, 10), | |
66 | (4, 3), | |
67 | (5, 4), | |
e4f82268 | 68 | (16, 9), |
91b2e171 BV |
69 | ] |
70 | ||
5cdc02d4 | 71 | # Annotation classes |
91b2e171 BV |
72 | ANN_FIELDS = 0 |
73 | ANN_SECTIONS = 1 | |
74 | ||
75 | class Decoder(srd.Decoder): | |
2ba06442 | 76 | api_version = 3 |
91b2e171 BV |
77 | id = 'edid' |
78 | name = 'EDID' | |
b7a7e6f5 | 79 | longname = 'Extended Display Identification Data' |
a465436e | 80 | desc = 'Data structure describing display device capabilities.' |
91b2e171 | 81 | license = 'gplv3+' |
9e1437a0 | 82 | inputs = ['i2c'] |
3f5f3a92 UH |
83 | outputs = [] |
84 | tags = ['Display', 'Memory', 'PC'] | |
da9bcbd9 BV |
85 | annotations = ( |
86 | ('fields', 'EDID structure fields'), | |
87 | ('sections', 'EDID structure sections'), | |
88 | ) | |
be42cffc | 89 | annotation_rows = ( |
be42cffc | 90 | ('fields', 'Fields', (0,)), |
3f5f3a92 | 91 | ('sections', 'Sections', (1,)), |
be42cffc | 92 | ) |
91b2e171 | 93 | |
92b7b49f | 94 | def __init__(self): |
2ba06442 UH |
95 | self.reset() |
96 | ||
97 | def reset(self): | |
91b2e171 BV |
98 | self.state = None |
99 | # Received data items, used as an index into samplenum/data | |
100 | self.cnt = 0 | |
101 | # Start/end sample numbers per data item | |
102 | self.sn = [] | |
103 | # Received data | |
104 | self.cache = [] | |
fb249641 UH |
105 | # Random read offset |
106 | self.offset = 0 | |
107 | # Extensions | |
108 | self.extension = 0 | |
109 | self.ext_sn = [[]] | |
110 | self.ext_cache = [[]] | |
91b2e171 | 111 | |
8915b346 | 112 | def start(self): |
be465111 | 113 | self.out_ann = self.register(srd.OUTPUT_ANN) |
91b2e171 BV |
114 | |
115 | def decode(self, ss, es, data): | |
48eee789 UH |
116 | cmd, data = data |
117 | ||
fb249641 UH |
118 | if cmd == 'ADDRESS WRITE' and data == 0x50: |
119 | self.state = 'offset' | |
120 | self.ss = ss | |
121 | return | |
122 | ||
123 | if cmd == 'ADDRESS READ' and data == 0x50: | |
124 | if self.extension > 0: | |
125 | self.state = 'extensions' | |
126 | s = str(self.extension) | |
127 | t = ["Extension: " + s, "X: " + s, s] | |
128 | else: | |
129 | self.state = 'header' | |
130 | t = ["EDID"] | |
131 | self.put(ss, es, self.out_ann, [ANN_SECTIONS, t]) | |
132 | return | |
133 | ||
134 | if cmd == 'DATA WRITE' and self.state == 'offset': | |
135 | self.offset = data | |
136 | self.extension = self.offset // 128 | |
137 | self.cnt = self.offset % 128 | |
138 | if self.extension > 0: | |
139 | ext = self.extension - 1 | |
140 | l = len(self.ext_sn[ext]) | |
141 | # Truncate or extend to self.cnt. | |
142 | self.sn = self.ext_sn[ext][0:self.cnt] + [0] * max(0, self.cnt - l) | |
143 | self.cache = self.ext_cache[ext][0:self.cnt] + [0] * max(0, self.cnt - l) | |
144 | else: | |
145 | l = len(self.sn) | |
146 | self.sn = self.sn[0:self.cnt] + [0] * max(0, self.cnt - l) | |
147 | self.cache = self.cache[0:self.cnt] + [0] * max(0, self.cnt - l) | |
148 | ss = self.ss if self.ss else ss | |
149 | s = str(data) | |
150 | t = ["Offset: " + s, "O: " + s, s] | |
151 | self.put(ss, es, self.out_ann, [ANN_SECTIONS, t]) | |
152 | return | |
153 | ||
48eee789 UH |
154 | # We only care about actual data bytes that are read (for now). |
155 | if cmd != 'DATA READ': | |
156 | return | |
157 | ||
91b2e171 | 158 | self.cnt += 1 |
fb249641 UH |
159 | if self.extension > 0: |
160 | self.ext_sn[self.extension - 1].append([ss, es]) | |
161 | self.ext_cache[self.extension - 1].append(data) | |
162 | else: | |
163 | self.sn.append([ss, es]) | |
164 | self.cache.append(data) | |
91b2e171 | 165 | |
fb249641 | 166 | if self.state is None or self.state == 'header': |
91b2e171 BV |
167 | # Wait for the EDID header |
168 | if self.cnt >= OFF_VENDOR: | |
169 | if self.cache[-8:] == EDID_HEADER: | |
170 | # Throw away any garbage before the header | |
171 | self.sn = self.sn[-8:] | |
172 | self.cache = self.cache[-8:] | |
2a706c20 | 173 | self.cnt = 8 |
91b2e171 | 174 | self.state = 'edid' |
2ef80066 BV |
175 | self.put(self.sn[0][0], es, self.out_ann, |
176 | [ANN_SECTIONS, ['Header']]) | |
177 | self.put(self.sn[0][0], es, self.out_ann, | |
178 | [ANN_FIELDS, ['Header pattern']]) | |
91b2e171 BV |
179 | elif self.state == 'edid': |
180 | if self.cnt == OFF_VERSION: | |
181 | self.decode_vid(-10) | |
182 | self.decode_pid(-8) | |
183 | self.decode_serial(-6) | |
184 | self.decode_mfrdate(-2) | |
2ef80066 BV |
185 | self.put(self.sn[OFF_VENDOR][0], es, self.out_ann, |
186 | [ANN_SECTIONS, ['Vendor/product']]) | |
91b2e171 | 187 | elif self.cnt == OFF_BASIC: |
2ef80066 BV |
188 | self.put(self.sn[OFF_VERSION][0], es, self.out_ann, |
189 | [ANN_SECTIONS, ['EDID Version']]) | |
190 | self.put(self.sn[OFF_VERSION][0], self.sn[OFF_VERSION][1], | |
191 | self.out_ann, [ANN_FIELDS, | |
a9f7935a | 192 | ['Version %d' % self.cache[-2]]]) |
2ef80066 BV |
193 | self.put(self.sn[OFF_VERSION+1][0], self.sn[OFF_VERSION+1][1], |
194 | self.out_ann, [ANN_FIELDS, | |
a9f7935a | 195 | ['Revision %d' % self.cache[-1]]]) |
91b2e171 | 196 | elif self.cnt == OFF_CHROM: |
2ef80066 BV |
197 | self.put(self.sn[OFF_BASIC][0], es, self.out_ann, |
198 | [ANN_SECTIONS, ['Basic display']]) | |
91b2e171 BV |
199 | self.decode_basicdisplay(-5) |
200 | elif self.cnt == OFF_EST_TIMING: | |
2ef80066 BV |
201 | self.put(self.sn[OFF_CHROM][0], es, self.out_ann, |
202 | [ANN_SECTIONS, ['Color characteristics']]) | |
91b2e171 BV |
203 | self.decode_chromaticity(-10) |
204 | elif self.cnt == OFF_STD_TIMING: | |
2ef80066 BV |
205 | self.put(self.sn[OFF_EST_TIMING][0], es, self.out_ann, |
206 | [ANN_SECTIONS, ['Established timings']]) | |
91b2e171 BV |
207 | self.decode_est_timing(-3) |
208 | elif self.cnt == OFF_DET_TIMING: | |
2ef80066 BV |
209 | self.put(self.sn[OFF_STD_TIMING][0], es, self.out_ann, |
210 | [ANN_SECTIONS, ['Standard timings']]) | |
211 | self.decode_std_timing(self.cnt - 16) | |
91b2e171 BV |
212 | elif self.cnt == OFF_NUM_EXT: |
213 | self.decode_descriptors(-72) | |
214 | elif self.cnt == OFF_CHECKSUM: | |
09059016 UH |
215 | self.put(ss, es, self.out_ann, |
216 | [0, ['Extensions present: %d' % self.cache[self.cnt-1]]]) | |
91b2e171 BV |
217 | elif self.cnt == OFF_CHECKSUM+1: |
218 | checksum = 0 | |
219 | for i in range(128): | |
220 | checksum += self.cache[i] | |
221 | if checksum % 256 == 0: | |
09059016 | 222 | csstr = 'OK' |
91b2e171 | 223 | else: |
09059016 UH |
224 | csstr = 'WRONG!' |
225 | self.put(ss, es, self.out_ann, [0, ['Checksum: %d (%s)' % ( | |
226 | self.cache[self.cnt-1], csstr)]]) | |
91b2e171 | 227 | self.state = 'extensions' |
fb249641 | 228 | |
91b2e171 | 229 | elif self.state == 'extensions': |
fb249641 UH |
230 | cache = self.ext_cache[self.extension - 1] |
231 | sn = self.ext_sn[self.extension - 1] | |
232 | v = cache[self.cnt - 1] | |
233 | if self.cnt == 1: | |
234 | if v == 2: | |
235 | self.put(ss, es, self.out_ann, [1, ['Extensions Tag', 'Tag']]) | |
236 | else: | |
237 | self.put(ss, es, self.out_ann, [1, ['Bad Tag']]) | |
238 | elif self.cnt == 2: | |
239 | self.put(ss, es, self.out_ann, [1, ['Version']]) | |
240 | self.put(ss, es, self.out_ann, [0, [str(v)]]) | |
241 | elif self.cnt == 3: | |
242 | self.put(ss, es, self.out_ann, [1, ['DTD offset']]) | |
243 | self.put(ss, es, self.out_ann, [0, [str(v)]]) | |
244 | elif self.cnt == 4: | |
245 | self.put(ss, es, self.out_ann, [1, ['Format support | DTD count']]) | |
246 | support = "Underscan: {0}, {1} Audio, YCbCr: {2}".format( | |
247 | "yes" if v & 0x80 else "no", | |
248 | "Basic" if v & 0x40 else "No", | |
249 | ["None", "422", "444", "422+444"][(v & 0x30) >> 4]) | |
250 | self.put(ss, es, self.out_ann, [0, ['{0}, DTDs: {1}'.format(support, v & 0xf)]]) | |
251 | elif self.cnt <= cache[2]: | |
252 | if self.cnt == cache[2]: | |
253 | self.put(sn[4][0], es, self.out_ann, [1, ['Data block collection']]) | |
254 | self.decode_data_block_collection(cache[4:], sn[4:]) | |
255 | elif (self.cnt - cache[2]) % 18 == 0: | |
256 | n = (self.cnt - cache[2]) / 18 | |
257 | if n <= cache[3] & 0xf: | |
258 | self.put(sn[self.cnt - 18][0], es, self.out_ann, [1, ['DTD']]) | |
259 | self.decode_descriptors(-18) | |
260 | ||
261 | elif self.cnt == 127: | |
262 | dtd_last = cache[2] + (cache[3] & 0xf) * 18 | |
263 | self.put(sn[dtd_last][0], es, self.out_ann, [1, ['Padding']]) | |
264 | elif self.cnt == 128: | |
265 | checksum = sum(cache) % 256 | |
266 | self.put(ss, es, self.out_ann, [0, ['Checksum: %d (%s)' % ( | |
267 | cache[self.cnt-1], 'Wrong' if checksum else 'OK')]]) | |
91b2e171 BV |
268 | |
269 | def ann_field(self, start, end, annotation): | |
fb249641 UH |
270 | annotation = annotation if isinstance(annotation, list) else [annotation] |
271 | sn = self.ext_sn[self.extension - 1] if self.extension else self.sn | |
272 | self.put(sn[start][0], sn[end][1], | |
273 | self.out_ann, [ANN_FIELDS, annotation]) | |
91b2e171 BV |
274 | |
275 | def lookup_pnpid(self, pnpid): | |
1063646c | 276 | pnpid_file = os.path.join(os.path.dirname(__file__), 'pnpids.txt') |
91b2e171 BV |
277 | if os.path.exists(pnpid_file): |
278 | for line in open(pnpid_file).readlines(): | |
279 | if line.find(pnpid + ';') == 0: | |
280 | return line[4:].strip() | |
281 | return '' | |
282 | ||
283 | def decode_vid(self, offset): | |
284 | pnpid = chr(64 + ((self.cache[offset] & 0x7c) >> 2)) | |
285 | pnpid += chr(64 + (((self.cache[offset] & 0x03) << 3) | |
286 | | ((self.cache[offset+1] & 0xe0) >> 5))) | |
287 | pnpid += chr(64 + (self.cache[offset+1] & 0x1f)) | |
288 | vendor = self.lookup_pnpid(pnpid) | |
289 | if vendor: | |
09059016 | 290 | pnpid += ' (%s)' % vendor |
91b2e171 BV |
291 | self.ann_field(offset, offset+1, pnpid) |
292 | ||
293 | def decode_pid(self, offset): | |
09059016 | 294 | pidstr = 'Product 0x%.2x%.2x' % (self.cache[offset+1], self.cache[offset]) |
91b2e171 BV |
295 | self.ann_field(offset, offset+1, pidstr) |
296 | ||
297 | def decode_serial(self, offset): | |
298 | serialnum = (self.cache[offset+3] << 24) \ | |
299 | + (self.cache[offset+2] << 16) \ | |
300 | + (self.cache[offset+1] << 8) \ | |
301 | + self.cache[offset] | |
302 | serialstr = '' | |
303 | is_alnum = True | |
304 | for i in range(4): | |
305 | if not chr(self.cache[offset+3-i]).isalnum(): | |
306 | is_alnum = False | |
307 | break | |
308 | serialstr += chr(self.cache[offset+3-i]) | |
09059016 UH |
309 | serial = serialstr if is_alnum else str(serialnum) |
310 | self.ann_field(offset, offset+3, 'Serial ' + serial) | |
91b2e171 BV |
311 | |
312 | def decode_mfrdate(self, offset): | |
313 | datestr = '' | |
314 | if self.cache[offset]: | |
09059016 | 315 | datestr += 'week %d, ' % self.cache[offset] |
91b2e171 BV |
316 | datestr += str(1990 + self.cache[offset+1]) |
317 | if datestr: | |
fb249641 | 318 | self.ann_field(offset, offset+1, ['Manufactured ' + datestr, datestr]) |
91b2e171 BV |
319 | |
320 | def decode_basicdisplay(self, offset): | |
321 | # Video input definition | |
322 | vid = self.cache[offset] | |
323 | if vid & 0x80: | |
324 | # Digital | |
09059016 | 325 | self.ann_field(offset, offset, 'Video input: VESA DFP 1.') |
91b2e171 BV |
326 | else: |
327 | # Analog | |
328 | sls = (vid & 60) >> 5 | |
09059016 | 329 | self.ann_field(offset, offset, 'Signal level standard: %.2x' % sls) |
91b2e171 | 330 | if vid & 0x10: |
09059016 | 331 | self.ann_field(offset, offset, 'Blank-to-black setup expected') |
91b2e171 BV |
332 | syncs = '' |
333 | if vid & 0x08: | |
334 | syncs += 'separate syncs, ' | |
335 | if vid & 0x04: | |
336 | syncs += 'composite syncs, ' | |
337 | if vid & 0x02: | |
338 | syncs += 'sync on green, ' | |
339 | if vid & 0x01: | |
340 | syncs += 'Vsync serration required, ' | |
341 | if syncs: | |
09059016 | 342 | self.ann_field(offset, offset, 'Supported syncs: %s' % syncs[:-2]) |
91b2e171 BV |
343 | # Max horizontal/vertical image size |
344 | if self.cache[offset+1] != 0 and self.cache[offset+2] != 0: | |
345 | # Projectors have this set to 0 | |
09059016 UH |
346 | sizestr = '%dx%dcm' % (self.cache[offset+1], self.cache[offset+2]) |
347 | self.ann_field(offset+1, offset+2, 'Physical size: ' + sizestr) | |
91b2e171 BV |
348 | # Display transfer characteristic (gamma) |
349 | if self.cache[offset+3] != 0xff: | |
350 | gamma = (self.cache[offset+3] + 100) / 100 | |
09059016 | 351 | self.ann_field(offset+3, offset+3, 'Gamma: %1.2f' % gamma) |
91b2e171 BV |
352 | # Feature support |
353 | fs = self.cache[offset+4] | |
354 | dpms = '' | |
355 | if fs & 0x80: | |
356 | dpms += 'standby, ' | |
357 | if fs & 0x40: | |
358 | dpms += 'suspend, ' | |
359 | if fs & 0x20: | |
360 | dpms += 'active off, ' | |
361 | if dpms: | |
09059016 | 362 | self.ann_field(offset+4, offset+4, 'DPMS support: %s' % dpms[:-2]) |
91b2e171 BV |
363 | dt = (fs & 0x18) >> 3 |
364 | dtstr = '' | |
365 | if dt == 0: | |
366 | dtstr = 'Monochrome' | |
367 | elif dt == 1: | |
368 | dtstr = 'RGB color' | |
369 | elif dt == 2: | |
e4f82268 | 370 | dtstr = 'non-RGB multicolor' |
91b2e171 | 371 | if dtstr: |
09059016 | 372 | self.ann_field(offset+4, offset+4, 'Display type: %s' % dtstr) |
91b2e171 | 373 | if fs & 0x04: |
09059016 | 374 | self.ann_field(offset+4, offset+4, 'Color space: standard sRGB') |
91b2e171 BV |
375 | # Save this for when we decode the first detailed timing descriptor |
376 | self.have_preferred_timing = (fs & 0x02) == 0x02 | |
377 | if fs & 0x01: | |
378 | gft = '' | |
379 | else: | |
380 | gft = 'not ' | |
09059016 UH |
381 | self.ann_field(offset+4, offset+4, |
382 | 'Generalized timing formula: %ssupported' % gft) | |
91b2e171 BV |
383 | |
384 | def convert_color(self, value): | |
385 | # Convert from 10-bit packet format to float | |
386 | outval = 0.0 | |
387 | for i in range(10): | |
388 | if value & 0x01: | |
389 | outval += 2 ** -(10-i) | |
390 | value >>= 1 | |
391 | return outval | |
392 | ||
393 | def decode_chromaticity(self, offset): | |
394 | redx = (self.cache[offset+2] << 2) + ((self.cache[offset] & 0xc0) >> 6) | |
395 | redy = (self.cache[offset+3] << 2) + ((self.cache[offset] & 0x30) >> 4) | |
09059016 UH |
396 | self.ann_field(offset, offset+9, 'Chromacity red: X %1.3f, Y %1.3f' % ( |
397 | self.convert_color(redx), self.convert_color(redy))) | |
91b2e171 BV |
398 | |
399 | greenx = (self.cache[offset+4] << 2) + ((self.cache[offset] & 0x0c) >> 6) | |
400 | greeny = (self.cache[offset+5] << 2) + ((self.cache[offset] & 0x03) >> 4) | |
09059016 UH |
401 | self.ann_field(offset, offset+9, 'Chromacity green: X %1.3f, Y %1.3f' % ( |
402 | self.convert_color(greenx), self.convert_color(greeny))) | |
91b2e171 BV |
403 | |
404 | bluex = (self.cache[offset+6] << 2) + ((self.cache[offset+1] & 0xc0) >> 6) | |
405 | bluey = (self.cache[offset+7] << 2) + ((self.cache[offset+1] & 0x30) >> 4) | |
09059016 UH |
406 | self.ann_field(offset, offset+9, 'Chromacity blue: X %1.3f, Y %1.3f' % ( |
407 | self.convert_color(bluex), self.convert_color(bluey))) | |
91b2e171 BV |
408 | |
409 | whitex = (self.cache[offset+8] << 2) + ((self.cache[offset+1] & 0x0c) >> 6) | |
410 | whitey = (self.cache[offset+9] << 2) + ((self.cache[offset+1] & 0x03) >> 4) | |
09059016 UH |
411 | self.ann_field(offset, offset+9, 'Chromacity white: X %1.3f, Y %1.3f' % ( |
412 | self.convert_color(whitex), self.convert_color(whitey))) | |
91b2e171 BV |
413 | |
414 | def decode_est_timing(self, offset): | |
415 | # Pre-EDID modes | |
416 | bitmap = (self.cache[offset] << 9) \ | |
417 | + (self.cache[offset+1] << 1) \ | |
418 | + ((self.cache[offset+2] & 0x80) >> 7) | |
419 | modestr = '' | |
420 | for i in range(17): | |
421 | if bitmap & (1 << (16-i)): | |
422 | modestr += est_modes[i] + ', ' | |
423 | if modestr: | |
09059016 | 424 | self.ann_field(offset, offset+2, |
868fd207 | 425 | 'Supported established modes: %s' % modestr[:-2]) |
91b2e171 BV |
426 | |
427 | def decode_std_timing(self, offset): | |
428 | modestr = '' | |
429 | for i in range(0, 16, 2): | |
430 | if self.cache[offset+i] == 0x01 and self.cache[offset+i+1] == 0x01: | |
431 | # Unused field | |
432 | continue | |
433 | x = (self.cache[offset+i] + 31) * 8 | |
434 | ratio = (self.cache[offset+i+1] & 0xc0) >> 6 | |
435 | ratio_x, ratio_y = xy_ratio[ratio] | |
436 | y = x / ratio_x * ratio_y | |
437 | refresh = (self.cache[offset+i+1] & 0x3f) + 60 | |
09059016 | 438 | modestr += '%dx%d@%dHz, ' % (x, y, refresh) |
91b2e171 | 439 | if modestr: |
2ef80066 BV |
440 | self.ann_field(offset, offset + 15, |
441 | 'Supported standard modes: %s' % modestr[:-2]) | |
91b2e171 | 442 | |
fb249641 UH |
443 | def decode_detailed_timing(self, cache, sn, offset, is_first): |
444 | if is_first and self.have_preferred_timing: | |
91b2e171 BV |
445 | # Only on first detailed timing descriptor |
446 | section = 'Preferred' | |
447 | else: | |
448 | section = 'Detailed' | |
449 | section += ' timing descriptor' | |
fb249641 UH |
450 | |
451 | self.put(sn[0][0], sn[17][1], | |
91b2e171 BV |
452 | self.out_ann, [ANN_SECTIONS, [section]]) |
453 | ||
fb249641 | 454 | pixclock = float((cache[1] << 8) + cache[0]) / 100 |
09059016 | 455 | self.ann_field(offset, offset+1, 'Pixel clock: %.2f MHz' % pixclock) |
91b2e171 | 456 | |
fb249641 UH |
457 | horiz_active = ((cache[4] & 0xf0) << 4) + cache[2] |
458 | horiz_blank = ((cache[4] & 0x0f) << 8) + cache[3] | |
459 | self.ann_field(offset+2, offset+4, 'Horizontal active: %d, blanking: %d' % (horiz_active, horiz_blank)) | |
91b2e171 | 460 | |
fb249641 UH |
461 | vert_active = ((cache[7] & 0xf0) << 4) + cache[5] |
462 | vert_blank = ((cache[7] & 0x0f) << 8) + cache[6] | |
463 | self.ann_field(offset+5, offset+7, 'Vertical active: %d, blanking: %d' % (vert_active, vert_blank)) | |
91b2e171 | 464 | |
fb249641 UH |
465 | horiz_sync_off = ((cache[11] & 0xc0) << 2) + cache[8] |
466 | horiz_sync_pw = ((cache[11] & 0x30) << 4) + cache[9] | |
467 | vert_sync_off = ((cache[11] & 0x0c) << 2) + ((cache[10] & 0xf0) >> 4) | |
468 | vert_sync_pw = ((cache[11] & 0x03) << 4) + (cache[10] & 0x0f) | |
91b2e171 | 469 | |
fb249641 UH |
470 | syncs = (horiz_sync_off, horiz_sync_pw, vert_sync_off, vert_sync_pw) |
471 | self.ann_field(offset+8, offset+11, [ | |
472 | 'Horizontal sync offset: %d, pulse width: %d, Vertical sync offset: %d, pulse width: %d' % syncs, | |
473 | 'HSync off: %d, pw: %d, VSync off: %d, pw: %d' % syncs]) | |
91b2e171 | 474 | |
fb249641 UH |
475 | horiz_size = ((cache[14] & 0xf0) << 4) + cache[12] |
476 | vert_size = ((cache[14] & 0x0f) << 8) + cache[13] | |
09059016 | 477 | self.ann_field(offset+12, offset+14, 'Physical size: %dx%dmm' % (horiz_size, vert_size)) |
91b2e171 | 478 | |
fb249641 | 479 | horiz_border = cache[15] |
2ef80066 | 480 | self.ann_field(offset+15, offset+15, 'Horizontal border: %d pixels' % horiz_border) |
fb249641 | 481 | vert_border = cache[16] |
2ef80066 | 482 | self.ann_field(offset+16, offset+16, 'Vertical border: %d lines' % vert_border) |
91b2e171 BV |
483 | |
484 | features = 'Flags: ' | |
fb249641 | 485 | if cache[17] & 0x80: |
91b2e171 | 486 | features += 'interlaced, ' |
fb249641 | 487 | stereo = (cache[17] & 0x60) >> 5 |
91b2e171 | 488 | if stereo: |
fb249641 | 489 | if cache[17] & 0x01: |
91b2e171 | 490 | features += '2-way interleaved stereo (' |
09059016 UH |
491 | features += ['right image on even lines', |
492 | 'left image on even lines', | |
91b2e171 BV |
493 | 'side-by-side'][stereo-1] |
494 | features += '), ' | |
495 | else: | |
496 | features += 'field sequential stereo (' | |
497 | features += ['right image on sync=1', 'left image on sync=1', | |
498 | '4-way interleaved'][stereo-1] | |
499 | features += '), ' | |
fb249641 UH |
500 | sync = (cache[17] & 0x18) >> 3 |
501 | sync2 = (cache[17] & 0x06) >> 1 | |
91b2e171 BV |
502 | posneg = ['negative', 'positive'] |
503 | features += 'sync type ' | |
504 | if sync == 0x00: | |
505 | features += 'analog composite (serrate on RGB)' | |
506 | elif sync == 0x01: | |
507 | features += 'bipolar analog composite (serrate on RGB)' | |
508 | elif sync == 0x02: | |
509 | features += 'digital composite (serrate on composite polarity ' \ | |
09059016 | 510 | + (posneg[sync2 & 0x01]) + ')' |
91b2e171 BV |
511 | elif sync == 0x03: |
512 | features += 'digital separate (' | |
2a706c20 | 513 | features += 'Vsync polarity ' + (posneg[(sync2 & 0x02) >> 1]) |
91b2e171 BV |
514 | features += ', Hsync polarity ' + (posneg[sync2 & 0x01]) |
515 | features += ')' | |
516 | features += ', ' | |
517 | self.ann_field(offset+17, offset+17, features[:-2]) | |
518 | ||
fb249641 UH |
519 | def decode_descriptor(self, cache, offset): |
520 | tag = cache[3] | |
521 | self.ann_field(offset, offset+1, "Flag") | |
522 | self.ann_field(offset+2, offset+2, "Flag (reserved)") | |
523 | self.ann_field(offset+3, offset+3, "Tag: {0:X}".format(tag)) | |
524 | self.ann_field(offset+4, offset+4, "Flag") | |
525 | ||
526 | sn = self.ext_sn[self.extension - 1] if self.extension else self.sn | |
527 | ||
91b2e171 BV |
528 | if tag == 0xff: |
529 | # Monitor serial number | |
fb249641 | 530 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, |
2ef80066 | 531 | [ANN_SECTIONS, ['Serial number']]) |
fb249641 UH |
532 | text = bytes(cache[5:][:13]).decode(encoding='cp437', errors='replace') |
533 | self.ann_field(offset+5, offset+17, text.strip()) | |
91b2e171 BV |
534 | elif tag == 0xfe: |
535 | # Text | |
fb249641 | 536 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, |
2ef80066 | 537 | [ANN_SECTIONS, ['Text']]) |
fb249641 UH |
538 | text = bytes(cache[5:][:13]).decode(encoding='cp437', errors='replace') |
539 | self.ann_field(offset+5, offset+17, text.strip()) | |
91b2e171 BV |
540 | elif tag == 0xfc: |
541 | # Monitor name | |
fb249641 | 542 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, |
2ef80066 | 543 | [ANN_SECTIONS, ['Monitor name']]) |
fb249641 UH |
544 | text = bytes(cache[5:][:13]).decode(encoding='cp437', errors='replace') |
545 | self.ann_field(offset+5, offset+17, text.strip()) | |
91b2e171 BV |
546 | elif tag == 0xfd: |
547 | # Monitor range limits | |
fb249641 | 548 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, |
09059016 | 549 | [ANN_SECTIONS, ['Monitor range limits']]) |
fb249641 UH |
550 | self.ann_field(offset+5, offset+5, [ |
551 | 'Minimum vertical rate: {0}Hz'.format(cache[5]), | |
552 | 'VSync >= {0}Hz'.format(cache[5])]) | |
553 | self.ann_field(offset+6, offset+6, [ | |
554 | 'Maximum vertical rate: {0}Hz'.format(cache[6]), | |
555 | 'VSync <= {0}Hz'.format(cache[6])]) | |
556 | self.ann_field(offset+7, offset+7, [ | |
557 | 'Minimum horizontal rate: {0}kHz'.format(cache[7]), | |
558 | 'HSync >= {0}kHz'.format(cache[7])]) | |
559 | self.ann_field(offset+8, offset+8, [ | |
560 | 'Maximum horizontal rate: {0}kHz'.format(cache[8]), | |
561 | 'HSync <= {0}kHz'.format(cache[8])]) | |
562 | self.ann_field(offset+9, offset+9, [ | |
563 | 'Maximum pixel clock: {0}MHz'.format(cache[9] * 10), | |
564 | 'PixClk <= {0}MHz'.format(cache[9] * 10)]) | |
565 | if cache[10] == 0x02: | |
566 | self.ann_field(offset+10, offset+10, ['Secondary timing formula supported', '2nd GTF: yes']) | |
567 | self.ann_field(offset+11, offset+17, ['GTF']) | |
568 | else: | |
569 | self.ann_field(offset+10, offset+10, ['Secondary timing formula unsupported', '2nd GTF: no']) | |
570 | self.ann_field(offset+11, offset+17, ['Padding']) | |
91b2e171 BV |
571 | elif tag == 0xfb: |
572 | # Additional color point data | |
fb249641 | 573 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, |
09059016 | 574 | [ANN_SECTIONS, ['Additional color point data']]) |
91b2e171 BV |
575 | elif tag == 0xfa: |
576 | # Additional standard timing definitions | |
fb249641 | 577 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, |
09059016 | 578 | [ANN_SECTIONS, ['Additional standard timing definitions']]) |
91b2e171 | 579 | else: |
fb249641 | 580 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, |
09059016 | 581 | [ANN_SECTIONS, ['Unknown descriptor']]) |
91b2e171 BV |
582 | |
583 | def decode_descriptors(self, offset): | |
584 | # 4 consecutive 18-byte descriptor blocks | |
fb249641 UH |
585 | cache = self.ext_cache[self.extension - 1] if self.extension else self.cache |
586 | sn = self.ext_sn[self.extension - 1] if self.extension else self.sn | |
587 | ||
91b2e171 | 588 | for i in range(offset, 0, 18): |
fb249641 UH |
589 | if cache[i] != 0 or cache[i+1] != 0: |
590 | self.decode_detailed_timing(cache[i:], sn[i:], i, i == offset) | |
591 | else: | |
592 | if cache[i+2] == 0 or cache[i+4] == 0: | |
593 | self.decode_descriptor(cache[i:], i) | |
594 | ||
595 | def decode_data_block(self, tag, cache, sn): | |
596 | codes = { 0: ['0: Reserved'], | |
597 | 1: ['1: Audio Data Block', 'Audio'], | |
598 | 2: ['2: Video Data Block', 'Video'], | |
599 | 3: ['3: Vendor Specific Data Block', 'VSDB'], | |
600 | 4: ['4: Speacker Allocation Data Block', 'SADB'], | |
601 | 5: ['5: VESA DTC Data Block', 'DTC'], | |
602 | 6: ['6: Reserved'], | |
603 | 7: ['7: Extended', 'Ext'] } | |
604 | ext_codes = { 0: [ '0: Video Capability Data Block', 'VCDB'], | |
605 | 1: [ '1: Vendor Specific Video Data Block', 'VSVDB'], | |
606 | 17: ['17: Vendor Specific Audio Data Block', 'VSADB'], } | |
607 | if tag < 7: | |
608 | code = codes[tag] | |
609 | ext_len = 0 | |
610 | if tag == 1: | |
611 | aformats = { 1: '1 (LPCM)' } | |
612 | rates = [ '192', '176', '96', '88', '48', '44', '32' ] | |
613 | ||
614 | aformat = cache[1] >> 3 | |
615 | sup_rates = [ i for i in range(0, 8) if (1 << i) & cache[2] ] | |
616 | ||
617 | data = "Format: {0} Channels: {1}".format( | |
618 | aformats.get(aformat, aformat), (cache[1] & 0x7) + 1) | |
619 | data += " Rates: " + " ".join(rates[6 - i] for i in sup_rates) | |
620 | data += " Extra: [{0:02X}]".format(cache[3]) | |
621 | ||
622 | elif tag ==2: | |
623 | data = "VIC: " | |
624 | data += ", ".join("{0}{1}".format(v & 0x7f, | |
625 | ['', ' (Native)'][v >> 7]) | |
626 | for v in cache[1:]) | |
627 | ||
628 | elif tag ==3: | |
629 | ouis = { b'\x00\x0c\x03': 'HDMI Licensing, LLC' } | |
630 | oui = bytes(cache[3:0:-1]) | |
631 | ouis = ouis.get(oui, None) | |
632 | data = "OUI: " + " ".join('{0:02X}'.format(x) for x in oui) | |
633 | data += " ({0})".format(ouis) if ouis else "" | |
634 | data += ", PhyAddr: {0}.{1}.{2}.{3}".format( | |
635 | cache[4] >> 4, cache[4] & 0xf, cache[5] >> 4, cache[5] & 0xf) | |
636 | data += ", [" + " ".join('{0:02X}'.format(x) for x in cache[6:]) + "]" | |
637 | ||
638 | elif tag ==4: | |
639 | speakers = [ 'FL/FR', 'LFE', 'FC', 'RL/RR', | |
640 | 'RC', 'FLC/FRC', 'RLC/RRC', 'FLW/FRW', | |
641 | 'FLH/FRH', 'TC', 'FCH' ] | |
642 | sup_speakers = cache[1] + (cache[2] << 8) | |
643 | sup_speakers = [ i for i in range(0, 8) if (1 << i) & sup_speakers ] | |
644 | data = "Speakers: " + " ".join(speakers[i] for i in sup_speakers) | |
645 | ||
91b2e171 | 646 | else: |
fb249641 UH |
647 | data = " ".join('{0:02X}'.format(x) for x in cache[1:]) |
648 | ||
649 | else: | |
650 | # Extended tags | |
651 | ext_len = 1 | |
652 | ext_code = ext_codes.get(cache[1], ['Unknown', '?']) | |
653 | code = zip(codes[7], [", ", ": "], ext_code) | |
654 | code = [ "".join(x) for x in code ] | |
655 | data = " ".join('{0:02X}'.format(x) for x in cache[2:]) | |
656 | ||
657 | self.put(sn[0][0], sn[0 + ext_len][1], self.out_ann, | |
658 | [ANN_FIELDS, code]) | |
659 | self.put(sn[1 + ext_len][0], sn[len(cache) - 1][1], self.out_ann, | |
660 | [ANN_FIELDS, [data]]) | |
661 | ||
662 | def decode_data_block_collection(self, cache, sn): | |
663 | offset = 0 | |
664 | while offset < len(cache): | |
665 | length = 1 + cache[offset] & 0x1f | |
666 | tag = cache[offset] >> 5 | |
667 | self.decode_data_block(tag, cache[offset:offset + length], sn[offset:]) | |
668 | offset += length |