]>
Commit | Line | Data |
---|---|---|
1 | ## | |
2 | ## This file is part of the libsigrokdecode project. | |
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 | ||
20 | # TODO: | |
21 | # - EDID < 1.3 | |
22 | # - add short annotations | |
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 | ||
42 | # Pre-EDID established timing modes | |
43 | est_modes = [ | |
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', | |
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), | |
68 | (16, 9), | |
69 | ] | |
70 | ||
71 | # Annotation classes | |
72 | ANN_FIELDS = 0 | |
73 | ANN_SECTIONS = 1 | |
74 | ||
75 | class Decoder(srd.Decoder): | |
76 | api_version = 3 | |
77 | id = 'edid' | |
78 | name = 'EDID' | |
79 | longname = 'Extended Display Identification Data' | |
80 | desc = 'Data structure describing display device capabilities.' | |
81 | license = 'gplv3+' | |
82 | inputs = ['i2c'] | |
83 | outputs = ['edid'] | |
84 | tags = ['Display', 'Memory', 'PC'] | |
85 | annotations = ( | |
86 | ('fields', 'EDID structure fields'), | |
87 | ('sections', 'EDID structure sections'), | |
88 | ) | |
89 | annotation_rows = ( | |
90 | ('sections', 'Sections', (1,)), | |
91 | ('fields', 'Fields', (0,)), | |
92 | ) | |
93 | ||
94 | def __init__(self): | |
95 | self.reset() | |
96 | ||
97 | def reset(self): | |
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 = [] | |
105 | # Random read offset | |
106 | self.offset = 0 | |
107 | # Extensions | |
108 | self.extension = 0 | |
109 | self.ext_sn = [[]] | |
110 | self.ext_cache = [[]] | |
111 | ||
112 | def start(self): | |
113 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
114 | ||
115 | def decode(self, ss, es, data): | |
116 | cmd, data = data | |
117 | ||
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 | ||
154 | # We only care about actual data bytes that are read (for now). | |
155 | if cmd != 'DATA READ': | |
156 | return | |
157 | ||
158 | self.cnt += 1 | |
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) | |
165 | ||
166 | if self.state is None or self.state == 'header': | |
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:] | |
173 | self.cnt = 8 | |
174 | self.state = 'edid' | |
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']]) | |
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) | |
185 | self.put(self.sn[OFF_VENDOR][0], es, self.out_ann, | |
186 | [ANN_SECTIONS, ['Vendor/product']]) | |
187 | elif self.cnt == OFF_BASIC: | |
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, | |
192 | ['Version %d' % self.cache[-2]]]) | |
193 | self.put(self.sn[OFF_VERSION+1][0], self.sn[OFF_VERSION+1][1], | |
194 | self.out_ann, [ANN_FIELDS, | |
195 | ['Revision %d' % self.cache[-1]]]) | |
196 | elif self.cnt == OFF_CHROM: | |
197 | self.put(self.sn[OFF_BASIC][0], es, self.out_ann, | |
198 | [ANN_SECTIONS, ['Basic display']]) | |
199 | self.decode_basicdisplay(-5) | |
200 | elif self.cnt == OFF_EST_TIMING: | |
201 | self.put(self.sn[OFF_CHROM][0], es, self.out_ann, | |
202 | [ANN_SECTIONS, ['Color characteristics']]) | |
203 | self.decode_chromaticity(-10) | |
204 | elif self.cnt == OFF_STD_TIMING: | |
205 | self.put(self.sn[OFF_EST_TIMING][0], es, self.out_ann, | |
206 | [ANN_SECTIONS, ['Established timings']]) | |
207 | self.decode_est_timing(-3) | |
208 | elif self.cnt == OFF_DET_TIMING: | |
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) | |
212 | elif self.cnt == OFF_NUM_EXT: | |
213 | self.decode_descriptors(-72) | |
214 | elif self.cnt == OFF_CHECKSUM: | |
215 | self.put(ss, es, self.out_ann, | |
216 | [0, ['Extensions present: %d' % self.cache[self.cnt-1]]]) | |
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: | |
222 | csstr = 'OK' | |
223 | else: | |
224 | csstr = 'WRONG!' | |
225 | self.put(ss, es, self.out_ann, [0, ['Checksum: %d (%s)' % ( | |
226 | self.cache[self.cnt-1], csstr)]]) | |
227 | self.state = 'extensions' | |
228 | ||
229 | elif self.state == 'extensions': | |
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')]]) | |
268 | ||
269 | def ann_field(self, start, end, annotation): | |
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]) | |
274 | ||
275 | def lookup_pnpid(self, pnpid): | |
276 | pnpid_file = os.path.join(os.path.dirname(__file__), 'pnpids.txt') | |
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: | |
290 | pnpid += ' (%s)' % vendor | |
291 | self.ann_field(offset, offset+1, pnpid) | |
292 | ||
293 | def decode_pid(self, offset): | |
294 | pidstr = 'Product 0x%.2x%.2x' % (self.cache[offset+1], self.cache[offset]) | |
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]) | |
309 | serial = serialstr if is_alnum else str(serialnum) | |
310 | self.ann_field(offset, offset+3, 'Serial ' + serial) | |
311 | ||
312 | def decode_mfrdate(self, offset): | |
313 | datestr = '' | |
314 | if self.cache[offset]: | |
315 | datestr += 'week %d, ' % self.cache[offset] | |
316 | datestr += str(1990 + self.cache[offset+1]) | |
317 | if datestr: | |
318 | self.ann_field(offset, offset+1, ['Manufactured ' + datestr, datestr]) | |
319 | ||
320 | def decode_basicdisplay(self, offset): | |
321 | # Video input definition | |
322 | vid = self.cache[offset] | |
323 | if vid & 0x80: | |
324 | # Digital | |
325 | self.ann_field(offset, offset, 'Video input: VESA DFP 1.') | |
326 | else: | |
327 | # Analog | |
328 | sls = (vid & 60) >> 5 | |
329 | self.ann_field(offset, offset, 'Signal level standard: %.2x' % sls) | |
330 | if vid & 0x10: | |
331 | self.ann_field(offset, offset, 'Blank-to-black setup expected') | |
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: | |
342 | self.ann_field(offset, offset, 'Supported syncs: %s' % syncs[:-2]) | |
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 | |
346 | sizestr = '%dx%dcm' % (self.cache[offset+1], self.cache[offset+2]) | |
347 | self.ann_field(offset+1, offset+2, 'Physical size: ' + sizestr) | |
348 | # Display transfer characteristic (gamma) | |
349 | if self.cache[offset+3] != 0xff: | |
350 | gamma = (self.cache[offset+3] + 100) / 100 | |
351 | self.ann_field(offset+3, offset+3, 'Gamma: %1.2f' % gamma) | |
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: | |
362 | self.ann_field(offset+4, offset+4, 'DPMS support: %s' % dpms[:-2]) | |
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: | |
370 | dtstr = 'non-RGB multicolor' | |
371 | if dtstr: | |
372 | self.ann_field(offset+4, offset+4, 'Display type: %s' % dtstr) | |
373 | if fs & 0x04: | |
374 | self.ann_field(offset+4, offset+4, 'Color space: standard sRGB') | |
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 ' | |
381 | self.ann_field(offset+4, offset+4, | |
382 | 'Generalized timing formula: %ssupported' % gft) | |
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) | |
396 | self.ann_field(offset, offset+9, 'Chromacity red: X %1.3f, Y %1.3f' % ( | |
397 | self.convert_color(redx), self.convert_color(redy))) | |
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) | |
401 | self.ann_field(offset, offset+9, 'Chromacity green: X %1.3f, Y %1.3f' % ( | |
402 | self.convert_color(greenx), self.convert_color(greeny))) | |
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) | |
406 | self.ann_field(offset, offset+9, 'Chromacity blue: X %1.3f, Y %1.3f' % ( | |
407 | self.convert_color(bluex), self.convert_color(bluey))) | |
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) | |
411 | self.ann_field(offset, offset+9, 'Chromacity white: X %1.3f, Y %1.3f' % ( | |
412 | self.convert_color(whitex), self.convert_color(whitey))) | |
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: | |
424 | self.ann_field(offset, offset+2, | |
425 | 'Supported established modes: %s' % modestr[:-2]) | |
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 | |
438 | modestr += '%dx%d@%dHz, ' % (x, y, refresh) | |
439 | if modestr: | |
440 | self.ann_field(offset, offset + 15, | |
441 | 'Supported standard modes: %s' % modestr[:-2]) | |
442 | ||
443 | def decode_detailed_timing(self, cache, sn, offset, is_first): | |
444 | if is_first and self.have_preferred_timing: | |
445 | # Only on first detailed timing descriptor | |
446 | section = 'Preferred' | |
447 | else: | |
448 | section = 'Detailed' | |
449 | section += ' timing descriptor' | |
450 | ||
451 | self.put(sn[0][0], sn[17][1], | |
452 | self.out_ann, [ANN_SECTIONS, [section]]) | |
453 | ||
454 | pixclock = float((cache[1] << 8) + cache[0]) / 100 | |
455 | self.ann_field(offset, offset+1, 'Pixel clock: %.2f MHz' % pixclock) | |
456 | ||
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)) | |
460 | ||
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)) | |
464 | ||
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) | |
469 | ||
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]) | |
474 | ||
475 | horiz_size = ((cache[14] & 0xf0) << 4) + cache[12] | |
476 | vert_size = ((cache[14] & 0x0f) << 8) + cache[13] | |
477 | self.ann_field(offset+12, offset+14, 'Physical size: %dx%dmm' % (horiz_size, vert_size)) | |
478 | ||
479 | horiz_border = cache[15] | |
480 | self.ann_field(offset+15, offset+15, 'Horizontal border: %d pixels' % horiz_border) | |
481 | vert_border = cache[16] | |
482 | self.ann_field(offset+16, offset+16, 'Vertical border: %d lines' % vert_border) | |
483 | ||
484 | features = 'Flags: ' | |
485 | if cache[17] & 0x80: | |
486 | features += 'interlaced, ' | |
487 | stereo = (cache[17] & 0x60) >> 5 | |
488 | if stereo: | |
489 | if cache[17] & 0x01: | |
490 | features += '2-way interleaved stereo (' | |
491 | features += ['right image on even lines', | |
492 | 'left image on even lines', | |
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 += '), ' | |
500 | sync = (cache[17] & 0x18) >> 3 | |
501 | sync2 = (cache[17] & 0x06) >> 1 | |
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 ' \ | |
510 | + (posneg[sync2 & 0x01]) + ')' | |
511 | elif sync == 0x03: | |
512 | features += 'digital separate (' | |
513 | features += 'Vsync polarity ' + (posneg[(sync2 & 0x02) >> 1]) | |
514 | features += ', Hsync polarity ' + (posneg[sync2 & 0x01]) | |
515 | features += ')' | |
516 | features += ', ' | |
517 | self.ann_field(offset+17, offset+17, features[:-2]) | |
518 | ||
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 | ||
528 | if tag == 0xff: | |
529 | # Monitor serial number | |
530 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, | |
531 | [ANN_SECTIONS, ['Serial number']]) | |
532 | text = bytes(cache[5:][:13]).decode(encoding='cp437', errors='replace') | |
533 | self.ann_field(offset+5, offset+17, text.strip()) | |
534 | elif tag == 0xfe: | |
535 | # Text | |
536 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, | |
537 | [ANN_SECTIONS, ['Text']]) | |
538 | text = bytes(cache[5:][:13]).decode(encoding='cp437', errors='replace') | |
539 | self.ann_field(offset+5, offset+17, text.strip()) | |
540 | elif tag == 0xfc: | |
541 | # Monitor name | |
542 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, | |
543 | [ANN_SECTIONS, ['Monitor name']]) | |
544 | text = bytes(cache[5:][:13]).decode(encoding='cp437', errors='replace') | |
545 | self.ann_field(offset+5, offset+17, text.strip()) | |
546 | elif tag == 0xfd: | |
547 | # Monitor range limits | |
548 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, | |
549 | [ANN_SECTIONS, ['Monitor range limits']]) | |
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']) | |
571 | elif tag == 0xfb: | |
572 | # Additional color point data | |
573 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, | |
574 | [ANN_SECTIONS, ['Additional color point data']]) | |
575 | elif tag == 0xfa: | |
576 | # Additional standard timing definitions | |
577 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, | |
578 | [ANN_SECTIONS, ['Additional standard timing definitions']]) | |
579 | else: | |
580 | self.put(sn[offset][0], sn[offset+17][1], self.out_ann, | |
581 | [ANN_SECTIONS, ['Unknown descriptor']]) | |
582 | ||
583 | def decode_descriptors(self, offset): | |
584 | # 4 consecutive 18-byte descriptor blocks | |
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 | ||
588 | for i in range(offset, 0, 18): | |
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 | ||
646 | else: | |
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 |