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