]> sigrok.org Git - libsigrokdecode.git/blob - decoders/edid/edid.py
edid: move PD docstring to __init__.py
[libsigrokdecode.git] / decoders / edid / edid.py
1 ##
2 ## This file is part of the sigrok 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
32 EDID_HEADER = [0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00]
33 OFF_VENDOR = 8
34 OFF_VERSION = 18
35 OFF_BASIC = 20
36 OFF_CHROM = 25
37 OFF_EST_TIMING = 35
38 OFF_STD_TIMING = 38
39 OFF_DET_TIMING = 54
40 OFF_NUM_EXT = 126
41 OFF_CHECKSUM = 127
42
43 # Pre-EDID established timing modes
44 est_modes = [
45     "720x400@70Hz",
46     "720x400@88Hz",
47     "640x480@60Hz",
48     "640x480@67Hz",
49     "640x480@72Hz",
50     "640x480@75Hz",
51     "800x600@56Hz",
52     "800x600@60Hz",
53     "800x600@72Hz",
54     "800x600@75Hz",
55     "832x624@75Hz",
56     "1024x768@87Hz(i)",
57     "1024x768@60Hz",
58     "1024x768@70Hz",
59     "1024x768@75Hz",
60     "1280x1024@75Hz",
61     "1152x870@75Hz"
62 ]
63
64 # X:Y display aspect ratios, as used in standard timing modes
65 xy_ratio = [
66     (16, 10),
67     (4, 3),
68     (5, 4),
69     (16, 9)
70 ]
71
72 # Annotation types
73 ANN_FIELDS = 0
74 ANN_SECTIONS = 1
75
76
77 class Decoder(srd.Decoder):
78     api_version = 1
79     id = 'edid'
80     name = 'EDID'
81     longname = 'Extended display identification data'
82     desc = 'A data structure describing the capabilities of a display device.'
83     license = 'gplv3+'
84     inputs = ['ddc2']
85     outputs = ['edid']
86     options = {}
87     annotations = [
88         ['EDID fields', 'EDID structure fields'],
89         ['EDID sections', 'EDID structure sections'],
90     ]
91
92     def __init__(self, **kwargs):
93         self.state = None
94         # Received data items, used as an index into samplenum/data
95         self.cnt = 0
96         # Start/end sample numbers per data item
97         self.sn = []
98         # Received data
99         self.cache = []
100
101     def start(self, metadata):
102         self.out_ann = self.add(srd.OUTPUT_ANN, 'edid')
103
104     def decode(self, ss, es, data):
105         if type(data) != int:
106             raise Exception('malformed ddc2 input: expected 1 byte')
107         self.cnt += 1
108         self.sn.append( [ss, es] )
109         self.cache.append(data)
110
111         if self.state is None:
112             # Wait for the EDID header
113             if self.cnt >= OFF_VENDOR:
114                 if self.cache[-8:] == EDID_HEADER:
115                     # Throw away any garbage before the header
116                     self.sn = self.sn[-8:]
117                     self.cache = self.cache[-8:]
118                     self.state = 'edid'
119                     self.put(ss, es, self.out_ann, [0, ["EDID header"]])
120         elif self.state == 'edid':
121             if self.cnt == OFF_VERSION:
122                 self.decode_vid(-10)
123                 self.decode_pid(-8)
124                 self.decode_serial(-6)
125                 self.decode_mfrdate(-2)
126             elif self.cnt == OFF_BASIC:
127                 version = "EDID version: %d.%d" % (self.cache[-2], self.cache[-1])
128                 self.put(ss, es, self.out_ann, [0, [version]])
129             elif self.cnt == OFF_CHROM:
130                 self.decode_basicdisplay(-5)
131             elif self.cnt == OFF_EST_TIMING:
132                 self.decode_chromaticity(-10)
133             elif self.cnt == OFF_STD_TIMING:
134                 self.decode_est_timing(-3)
135             elif self.cnt == OFF_DET_TIMING:
136                 self.decode_std_timing(-16)
137             elif self.cnt == OFF_NUM_EXT:
138                 self.decode_descriptors(-72)
139             elif self.cnt == OFF_CHECKSUM:
140                 self.put(ss, es, self.out_ann, [0, ["Extensions present: %d" % self.cache[self.cnt-1]]])
141             elif self.cnt == OFF_CHECKSUM+1:
142                 checksum = 0
143                 for i in range(128):
144                     checksum += self.cache[i]
145                 if checksum % 256 == 0:
146                     csstr = "OK"
147                 else:
148                     csstr = "WRONG!"
149                 self.put(ss, es, self.out_ann, [0, ["Checksum: %d (%s)" % (
150                                 self.cache[self.cnt-1], csstr)]])
151                 self.state = 'extensions'
152         elif self.state == 'extensions':
153             pass
154
155     def ann_field(self, start, end, annotation):
156         self.put(self.sn[start][0], self.sn[end][1], self.out_ann, [ANN_FIELDS, [annotation]])
157
158     def lookup_pnpid(self, pnpid):
159         pnpid_file = os.path.join(os.path.dirname(__file__), 'pnpids.txt')
160         if os.path.exists(pnpid_file):
161             for line in open(pnpid_file).readlines():
162                 if line.find(pnpid + ';') == 0:
163                     return line[4:].strip()
164         return ''
165
166     def decode_vid(self, offset):
167         pnpid = chr(64 + ((self.cache[offset] & 0x7c) >> 2))
168         pnpid += chr(64 + (((self.cache[offset] & 0x03) << 3)
169                            | ((self.cache[offset+1] & 0xe0) >> 5)))
170         pnpid += chr(64 + (self.cache[offset+1] & 0x1f))
171         vendor = self.lookup_pnpid(pnpid)
172         if vendor:
173             pnpid += " (%s)" % vendor
174         self.ann_field(offset, offset+1, pnpid)
175
176     def decode_pid(self, offset):
177         pidstr = "Product 0x%.2x%.2x" % (self.cache[offset+1], self.cache[offset])
178         self.ann_field(offset, offset+1, pidstr)
179
180     def decode_serial(self, offset):
181         serialnum = (self.cache[offset+3] << 24) \
182                 + (self.cache[offset+2] << 16) \
183                 + (self.cache[offset+1] << 8) \
184                 + self.cache[offset]
185         serialstr = ''
186         is_alnum = True
187         for i in range(4):
188             if not chr(self.cache[offset+3-i]).isalnum():
189                 is_alnum = False
190                 break
191             serialstr += chr(self.cache[offset+3-i])
192         if is_alnum:
193             serial = serialstr
194         else:
195             serial = str(serialnum)
196         self.ann_field(offset, offset+3, "Serial " + serial)
197
198     def decode_mfrdate(self, offset):
199         datestr = ''
200         if self.cache[offset]:
201             datestr += "week %d, " % self.cache[offset]
202         datestr += str(1990 + self.cache[offset+1])
203         if datestr:
204             self.ann_field(offset, offset+1, "Manufactured " + datestr)
205
206     def decode_basicdisplay(self, offset):
207         # Video input definition
208         vid = self.cache[offset]
209         if vid & 0x80:
210             # Digital
211             self.ann_field(offset, offset, "Video input: VESA DFP 1.")
212         else:
213             # Analog
214             sls = (vid & 60) >> 5
215             self.ann_field(offset, offset, "Signal level standard: %.2x" % sls)
216             if vid & 0x10:
217                 self.ann_field(offset, offset, "Blank-to-black setup expected")
218             syncs = ''
219             if vid & 0x08:
220                 syncs += 'separate syncs, '
221             if vid & 0x04:
222                 syncs += 'composite syncs, '
223             if vid & 0x02:
224                 syncs += 'sync on green, '
225             if vid & 0x01:
226                 syncs += 'Vsync serration required, '
227             if syncs:
228                 self.ann_field(offset, offset, "Supported syncs: %s" % syncs[:-2])
229         # Max horizontal/vertical image size
230         if self.cache[offset+1] != 0 and self.cache[offset+2] != 0:
231             # Projectors have this set to 0
232             sizestr = "%dx%dcm" % (self.cache[offset+1], self.cache[offset+2])
233             self.ann_field(offset+1, offset+2, "Physical size: " + sizestr)
234         # Display transfer characteristic (gamma)
235         if self.cache[offset+3] != 0xff:
236             gamma = (self.cache[offset+3] + 100) / 100
237             self.ann_field(offset+3, offset+3, "Gamma: %1.2f" % gamma)
238         # Feature support
239         fs = self.cache[offset+4]
240         dpms = ''
241         if fs & 0x80:
242             dpms += 'standby, '
243         if fs & 0x40:
244             dpms += 'suspend, '
245         if fs & 0x20:
246             dpms += 'active off, '
247         if dpms:
248             self.ann_field(offset+4, offset+4, "DPMS support: %s" % dpms[:-2])
249         dt = (fs & 0x18) >> 3
250         dtstr = ''
251         if dt == 0:
252             dtstr = 'Monochrome'
253         elif dt == 1:
254             dtstr = 'RGB color'
255         elif dt == 2:
256             dtstr = 'non-RGB multicolor' 
257         if dtstr:
258             self.ann_field(offset+4, offset+4, "Display type: %s" % dtstr)
259         if fs & 0x04:
260             self.ann_field(offset+4, offset+4, "Color space: standard sRGB")
261         # Save this for when we decode the first detailed timing descriptor
262         self.have_preferred_timing = (fs & 0x02) == 0x02
263         if fs & 0x01:
264             gft = ''
265         else:
266             gft = 'not '
267         self.ann_field(offset+4, offset+4, "Generalized timing formula: %ssupported" % gft)
268
269     def convert_color(self, value):
270         # Convert from 10-bit packet format to float
271         outval = 0.0
272         for i in range(10):
273             if value & 0x01:
274                 outval += 2 ** -(10-i)
275             value >>= 1
276         return outval
277
278     def decode_chromaticity(self, offset):
279         redx = (self.cache[offset+2] << 2) + ((self.cache[offset] & 0xc0) >> 6)
280         redy = (self.cache[offset+3] << 2) + ((self.cache[offset] & 0x30) >> 4)
281         self.ann_field(offset, offset+9, "Chromacity red: X %1.3f, Y %1.3f" % (
282                         self.convert_color(redx), self.convert_color(redy)))
283
284         greenx = (self.cache[offset+4] << 2) + ((self.cache[offset] & 0x0c) >> 6)
285         greeny = (self.cache[offset+5] << 2) + ((self.cache[offset] & 0x03) >> 4)
286         self.ann_field(offset, offset+9, "Chromacity green: X %1.3f, Y %1.3f" % (
287                         self.convert_color(greenx), self.convert_color(greeny)))
288
289         bluex = (self.cache[offset+6] << 2) + ((self.cache[offset+1] & 0xc0) >> 6)
290         bluey = (self.cache[offset+7] << 2) + ((self.cache[offset+1] & 0x30) >> 4)
291         self.ann_field(offset, offset+9, "Chromacity blue: X %1.3f, Y %1.3f" % (
292                         self.convert_color(bluex), self.convert_color(bluey)))
293
294         whitex = (self.cache[offset+8] << 2) + ((self.cache[offset+1] & 0x0c) >> 6)
295         whitey = (self.cache[offset+9] << 2) + ((self.cache[offset+1] & 0x03) >> 4)
296         self.ann_field(offset, offset+9, "Chromacity white: X %1.3f, Y %1.3f" % (
297                         self.convert_color(whitex), self.convert_color(whitey)))
298
299     def decode_est_timing(self, offset):
300         # Pre-EDID modes
301         bitmap = (self.cache[offset] << 9) \
302             + (self.cache[offset+1] << 1) \
303             + ((self.cache[offset+2] & 0x80) >> 7)
304         modestr = ''
305         for i in range(17):
306                 if bitmap & (1 << (16-i)):
307                     modestr += est_modes[i] + ', '
308         if modestr:
309             self.ann_field(offset, offset+2, "Supported establised modes: %s" % modestr[:-2])
310
311     def decode_std_timing(self, offset):
312         modestr = ''
313         for i in range(0, 16, 2):
314             if self.cache[offset+i] == 0x01 and self.cache[offset+i+1] == 0x01:
315                 # Unused field
316                 continue
317             x = (self.cache[offset+i] + 31) * 8
318             ratio = (self.cache[offset+i+1] & 0xc0) >> 6
319             ratio_x, ratio_y = xy_ratio[ratio]
320             y = x / ratio_x * ratio_y
321             refresh = (self.cache[offset+i+1] & 0x3f) + 60
322             modestr += "%dx%d@%dHz, " % (x, y, refresh)
323         if modestr:
324             self.ann_field(offset, offset+2, "Supported standard modes: %s" % modestr[:-2])
325
326     def decode_detailed_timing(self, offset):
327         if offset == -72 and self.have_preferred_timing:
328             # Only on first detailed timing descriptor
329             section = 'Preferred'
330         else:
331             section = 'Detailed'
332         section += ' timing descriptor'
333         self.put(self.sn[offset][0], self.sn[offset+18][1],
334              self.out_ann, [ANN_SECTIONS, [section]])
335
336         pixclock = float((self.cache[offset+1] << 8) + self.cache[offset]) / 100
337         self.ann_field(offset, offset+1, "Pixel clock: %.2f MHz" % pixclock)
338
339         horiz_active = ((self.cache[offset+4] & 0xf0) << 4) + self.cache[offset+2]
340         self.ann_field(offset+2, offset+4, "Horizontal active: %d" % horiz_active)
341
342         horiz_blank = ((self.cache[offset+4] & 0x0f) << 8) + self.cache[offset+3]
343         self.ann_field(offset+3, offset+4, "Horizontal blanking: %d" % horiz_blank)
344
345         vert_active = ((self.cache[offset+7] & 0xf0) << 4) + self.cache[offset+5]
346         self.ann_field(offset+5, offset+7, "Vertical active: %d" % vert_active)
347
348         vert_blank = ((self.cache[offset+7] & 0x0f) << 8) + self.cache[offset+6]
349         self.ann_field(offset+6, offset+7, "Vertical blanking: %d" % vert_blank)
350
351         horiz_sync_off = ((self.cache[offset+11] & 0xc0) << 2) + self.cache[offset+8]
352         self.ann_field(offset+8, offset+11, "Horizontal sync offset: %d" % horiz_sync_off)
353
354         horiz_sync_pw = ((self.cache[offset+11] & 0x30) << 4) + self.cache[offset+9]
355         self.ann_field(offset+9, offset+11, "Horizontal sync pulse width: %d" % horiz_sync_pw)
356
357         vert_sync_off = ((self.cache[offset+11] & 0x0c) << 2) \
358                     + ((self.cache[offset+10] & 0xf0) >> 4)
359         self.ann_field(offset+10, offset+11, "Vertical sync offset: %d" % vert_sync_off)
360
361         vert_sync_pw = ((self.cache[offset+11] & 0x03) << 4) \
362                     + (self.cache[offset+10] & 0x0f)
363         self.ann_field(offset+10, offset+11, "Vertical sync pulse width: %d" % vert_sync_pw)
364
365         horiz_size = ((self.cache[offset+14] & 0xf0) << 4) + self.cache[offset+12]
366         vert_size = ((self.cache[offset+14] & 0x0f) << 8) + self.cache[offset+13]
367         self.ann_field(offset+12, offset+14, "Physical size: %dx%dmm" % (horiz_size, vert_size))
368
369         horiz_border = self.cache[offset+15]
370         if horiz_border:
371             self.ann_field(offset+15, offset+15, "Horizontal border: %d pixels" % horiz_border)
372         vert_border = self.cache[offset+16]
373         if vert_border:
374             self.ann_field(offset+16, offset+16, "Vertical border: %d lines" % vert_border)
375
376         features = 'Flags: '
377         if self.cache[offset+17] & 0x80:
378             features += 'interlaced, '
379         stereo = (self.cache[offset+17] & 0x60) >> 5
380         if stereo:
381             if self.cache[offset+17] & 0x01:
382                 features += '2-way interleaved stereo ('
383                 features += ['right image on even lines', 'left image on even lines',
384                              'side-by-side'][stereo-1]
385                 features += '), '
386             else:
387                 features += 'field sequential stereo ('
388                 features += ['right image on sync=1', 'left image on sync=1',
389                              '4-way interleaved'][stereo-1]
390                 features += '), '
391         sync = (self.cache[offset+17] & 0x18) >> 3
392         sync2 = (self.cache[offset+17] & 0x06) >> 1
393         posneg = ['negative', 'positive']
394         features += 'sync type '
395         if sync == 0x00:
396             features += 'analog composite (serrate on RGB)'
397         elif sync == 0x01:
398             features += 'bipolar analog composite (serrate on RGB)'
399         elif sync == 0x02:
400             features += 'digital composite (serrate on composite polarity ' \
401                     + (posneg[sync2 & 0x01]) + ')'
402         elif sync == 0x03:
403             features += 'digital separate ('
404             features += 'Vsync polarity ' + (posneg[sync2 >> 1])
405             features += ', Hsync polarity ' + (posneg[sync2 & 0x01])
406             features += ')'
407         features += ', '
408         self.ann_field(offset+17, offset+17, features[:-2])
409
410     def decode_descriptor(self, offset):
411         tag = self.cache[offset+3]
412         if tag == 0xff:
413             # Monitor serial number
414             text = bytes(self.cache[offset+5:][:13]).decode(encoding='cp437', errors='replace')
415             self.ann_field(offset, offset+17, "Serial number: %s" % text.strip())
416         elif tag == 0xfe:
417             # Text
418             text = bytes(self.cache[offset+5:][:13]).decode(encoding='cp437', errors='replace')
419             self.ann_field(offset, offset+17, "Info: %s" % text.strip())
420         elif tag == 0xfc:
421             # Monitor name
422             text = bytes(self.cache[offset+5:][:13]).decode(encoding='cp437', errors='replace')
423             self.ann_field(offset, offset+17, "Model name: %s" % text.strip())
424         elif tag == 0xfd:
425             # Monitor range limits
426             self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
427                      [ANN_SECTIONS, ["Monitor range limits"]])
428             self.ann_field(offset+5, offset+5, "Minimum vertical rate: %dHz" % 
429                            self.cache[offset+5])
430             self.ann_field(offset+6, offset+6, "Maximum vertical rate: %dHz" % 
431                            self.cache[offset+6])
432             self.ann_field(offset+7, offset+7, "Minimum horizontal rate: %dkHz" % 
433                            self.cache[offset+7])
434             self.ann_field(offset+8, offset+8, "Maximum horizontal rate: %dkHz" % 
435                            self.cache[offset+8])
436             self.ann_field(offset+9, offset+9, "Maximum pixel clock: %dMHz" % 
437                            (self.cache[offset+9] * 10))
438             if self.cache[offset+10] == 0x02:
439                 # Secondary GTF curve supported
440                 self.ann_field(offset+10, offset+17, "Secondary timing formula supported")
441         elif tag == 0xfb:
442             # Additional color point data
443             self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
444                      [ANN_SECTIONS, ["Additional color point data"]])
445         elif tag == 0xfa:
446             # Additional standard timing definitions
447             self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
448                      [ANN_SECTIONS, ["Additional standard timing definitions"]])
449         else:
450             self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
451                      [ANN_SECTIONS, ["Unknown descriptor"]])
452
453     def decode_descriptors(self, offset):
454         # 4 consecutive 18-byte descriptor blocks
455         for i in range(offset, 0, 18):
456             if self.cache[i] != 0 and self.cache[i+1] != 0:
457                 self.decode_detailed_timing(i)
458             else:
459                 if self.cache[i+2] == 0 or self.cache[i+4] == 0:
460                     self.decode_descriptor(i)
461
462
463
464