2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
20 # EDID protocol decoder
24 # - add short annotations
25 # - Signal level standard field in basic display parameters block
26 # - Additional color point descriptors
27 # - Additional standard timing descriptors
30 import sigrokdecode as srd
33 EDID_HEADER = [0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00]
44 # Pre-EDID established timing modes
65 # X:Y display aspect ratios, as used in standard timing modes
77 class Decoder(srd.Decoder):
81 longname = 'Extended display identification data'
82 desc = 'A data structure describing the capabilities of a display device.'
88 ['EDID fields', 'EDID structure fields'],
89 ['EDID sections', 'EDID structure sections'],
92 def __init__(self, **kwargs):
94 # Received data items, used as an index into samplenum/data
96 # Start/end sample numbers per data item
101 def start(self, metadata):
102 self.out_ann = self.add(srd.OUTPUT_ANN, 'edid')
104 def decode(self, ss, es, data):
105 if type(data) != int:
106 raise Exception('malformed ddc2 input: expected 1 byte')
108 self.sn.append([ss, es])
109 self.cache.append(data)
111 # self.put(ss, es, self.out_ann, [0, ['%d: [%.2x]' % (self.cnt, data)]])
113 if self.state is None:
114 # Wait for the EDID header
115 if self.cnt >= OFF_VENDOR:
116 if self.cache[-8:] == EDID_HEADER:
117 # Throw away any garbage before the header
118 self.sn = self.sn[-8:]
119 self.cache = self.cache[-8:]
122 self.put(ss, es, self.out_ann, [0, ['EDID header']])
123 elif self.state == 'edid':
124 if self.cnt == OFF_VERSION:
127 self.decode_serial(-6)
128 self.decode_mfrdate(-2)
129 elif self.cnt == OFF_BASIC:
130 version = 'EDID version: %d.%d' % (self.cache[-2], self.cache[-1])
131 self.put(ss, es, self.out_ann, [0, [version]])
132 elif self.cnt == OFF_CHROM:
133 self.decode_basicdisplay(-5)
134 elif self.cnt == OFF_EST_TIMING:
135 self.decode_chromaticity(-10)
136 elif self.cnt == OFF_STD_TIMING:
137 self.decode_est_timing(-3)
138 elif self.cnt == OFF_DET_TIMING:
139 self.decode_std_timing(-16)
140 elif self.cnt == OFF_NUM_EXT:
141 self.decode_descriptors(-72)
142 elif self.cnt == OFF_CHECKSUM:
143 self.put(ss, es, self.out_ann,
144 [0, ['Extensions present: %d' % self.cache[self.cnt-1]]])
145 elif self.cnt == OFF_CHECKSUM+1:
148 checksum += self.cache[i]
149 if checksum % 256 == 0:
153 self.put(ss, es, self.out_ann, [0, ['Checksum: %d (%s)' % (
154 self.cache[self.cnt-1], csstr)]])
155 self.state = 'extensions'
156 elif self.state == 'extensions':
159 def ann_field(self, start, end, annotation):
160 self.put(self.sn[start][0], self.sn[end][1],
161 self.out_ann, [ANN_FIELDS, [annotation]])
163 def lookup_pnpid(self, pnpid):
164 pnpid_file = os.path.join(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()
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)
178 pnpid += ' (%s)' % vendor
179 self.ann_field(offset, offset+1, pnpid)
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)
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) \
193 if not chr(self.cache[offset+3-i]).isalnum():
196 serialstr += chr(self.cache[offset+3-i])
197 serial = serialstr if is_alnum else str(serialnum)
198 self.ann_field(offset, offset+3, 'Serial ' + serial)
200 def decode_mfrdate(self, offset):
202 if self.cache[offset]:
203 datestr += 'week %d, ' % self.cache[offset]
204 datestr += str(1990 + self.cache[offset+1])
206 self.ann_field(offset, offset+1, 'Manufactured ' + datestr)
208 def decode_basicdisplay(self, offset):
209 # Video input definition
210 vid = self.cache[offset]
213 self.ann_field(offset, offset, 'Video input: VESA DFP 1.')
216 sls = (vid & 60) >> 5
217 self.ann_field(offset, offset, 'Signal level standard: %.2x' % sls)
219 self.ann_field(offset, offset, 'Blank-to-black setup expected')
222 syncs += 'separate syncs, '
224 syncs += 'composite syncs, '
226 syncs += 'sync on green, '
228 syncs += 'Vsync serration required, '
230 self.ann_field(offset, offset, 'Supported syncs: %s' % syncs[:-2])
231 # Max horizontal/vertical image size
232 if self.cache[offset+1] != 0 and self.cache[offset+2] != 0:
233 # Projectors have this set to 0
234 sizestr = '%dx%dcm' % (self.cache[offset+1], self.cache[offset+2])
235 self.ann_field(offset+1, offset+2, 'Physical size: ' + sizestr)
236 # Display transfer characteristic (gamma)
237 if self.cache[offset+3] != 0xff:
238 gamma = (self.cache[offset+3] + 100) / 100
239 self.ann_field(offset+3, offset+3, 'Gamma: %1.2f' % gamma)
241 fs = self.cache[offset+4]
248 dpms += 'active off, '
250 self.ann_field(offset+4, offset+4, 'DPMS support: %s' % dpms[:-2])
251 dt = (fs & 0x18) >> 3
258 dtstr = 'non-RGB multicolor'
260 self.ann_field(offset+4, offset+4, 'Display type: %s' % dtstr)
262 self.ann_field(offset+4, offset+4, 'Color space: standard sRGB')
263 # Save this for when we decode the first detailed timing descriptor
264 self.have_preferred_timing = (fs & 0x02) == 0x02
269 self.ann_field(offset+4, offset+4,
270 'Generalized timing formula: %ssupported' % gft)
272 def convert_color(self, value):
273 # Convert from 10-bit packet format to float
277 outval += 2 ** -(10-i)
281 def decode_chromaticity(self, offset):
282 redx = (self.cache[offset+2] << 2) + ((self.cache[offset] & 0xc0) >> 6)
283 redy = (self.cache[offset+3] << 2) + ((self.cache[offset] & 0x30) >> 4)
284 self.ann_field(offset, offset+9, 'Chromacity red: X %1.3f, Y %1.3f' % (
285 self.convert_color(redx), self.convert_color(redy)))
287 greenx = (self.cache[offset+4] << 2) + ((self.cache[offset] & 0x0c) >> 6)
288 greeny = (self.cache[offset+5] << 2) + ((self.cache[offset] & 0x03) >> 4)
289 self.ann_field(offset, offset+9, 'Chromacity green: X %1.3f, Y %1.3f' % (
290 self.convert_color(greenx), self.convert_color(greeny)))
292 bluex = (self.cache[offset+6] << 2) + ((self.cache[offset+1] & 0xc0) >> 6)
293 bluey = (self.cache[offset+7] << 2) + ((self.cache[offset+1] & 0x30) >> 4)
294 self.ann_field(offset, offset+9, 'Chromacity blue: X %1.3f, Y %1.3f' % (
295 self.convert_color(bluex), self.convert_color(bluey)))
297 whitex = (self.cache[offset+8] << 2) + ((self.cache[offset+1] & 0x0c) >> 6)
298 whitey = (self.cache[offset+9] << 2) + ((self.cache[offset+1] & 0x03) >> 4)
299 self.ann_field(offset, offset+9, 'Chromacity white: X %1.3f, Y %1.3f' % (
300 self.convert_color(whitex), self.convert_color(whitey)))
302 def decode_est_timing(self, offset):
304 bitmap = (self.cache[offset] << 9) \
305 + (self.cache[offset+1] << 1) \
306 + ((self.cache[offset+2] & 0x80) >> 7)
309 if bitmap & (1 << (16-i)):
310 modestr += est_modes[i] + ', '
312 self.ann_field(offset, offset+2,
313 'Supported establised modes: %s' % modestr[:-2])
315 def decode_std_timing(self, offset):
317 for i in range(0, 16, 2):
318 if self.cache[offset+i] == 0x01 and self.cache[offset+i+1] == 0x01:
321 x = (self.cache[offset+i] + 31) * 8
322 ratio = (self.cache[offset+i+1] & 0xc0) >> 6
323 ratio_x, ratio_y = xy_ratio[ratio]
324 y = x / ratio_x * ratio_y
325 refresh = (self.cache[offset+i+1] & 0x3f) + 60
326 modestr += '%dx%d@%dHz, ' % (x, y, refresh)
328 self.ann_field(offset, offset+2,
329 'Supported standard modes: %s' % modestr[:-2])
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'
337 section += ' timing descriptor'
338 self.put(self.sn[offset][0], self.sn[offset+18][1],
339 self.out_ann, [ANN_SECTIONS, [section]])
341 pixclock = float((self.cache[offset+1] << 8) + self.cache[offset]) / 100
342 self.ann_field(offset, offset+1, 'Pixel clock: %.2f MHz' % pixclock)
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)
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)
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)
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)
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)
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)
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)
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)
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))
374 horiz_border = self.cache[offset+15]
376 self.ann_field(offset+15, offset+15, 'Horizontal border: %d pixels' % horiz_border)
377 vert_border = self.cache[offset+16]
379 self.ann_field(offset+16, offset+16, 'Vertical border: %d lines' % vert_border)
382 if self.cache[offset+17] & 0x80:
383 features += 'interlaced, '
384 stereo = (self.cache[offset+17] & 0x60) >> 5
386 if self.cache[offset+17] & 0x01:
387 features += '2-way interleaved stereo ('
388 features += ['right image on even lines',
389 'left image on even lines',
390 'side-by-side'][stereo-1]
393 features += 'field sequential stereo ('
394 features += ['right image on sync=1', 'left image on sync=1',
395 '4-way interleaved'][stereo-1]
397 sync = (self.cache[offset+17] & 0x18) >> 3
398 sync2 = (self.cache[offset+17] & 0x06) >> 1
399 posneg = ['negative', 'positive']
400 features += 'sync type '
402 features += 'analog composite (serrate on RGB)'
404 features += 'bipolar analog composite (serrate on RGB)'
406 features += 'digital composite (serrate on composite polarity ' \
407 + (posneg[sync2 & 0x01]) + ')'
409 features += 'digital separate ('
410 features += 'Vsync polarity ' + (posneg[(sync2 & 0x02) >> 1])
411 features += ', Hsync polarity ' + (posneg[sync2 & 0x01])
414 self.ann_field(offset+17, offset+17, features[:-2])
416 def decode_descriptor(self, offset):
417 tag = self.cache[offset+3]
419 # Monitor serial number
420 text = bytes(self.cache[offset+5:][:13]).decode(encoding='cp437', errors='replace')
421 self.ann_field(offset, offset+17, 'Serial number: %s' % text.strip())
424 text = bytes(self.cache[offset+5:][:13]).decode(encoding='cp437', errors='replace')
425 self.ann_field(offset, offset+17, 'Info: %s' % text.strip())
428 text = bytes(self.cache[offset+5:][:13]).decode(encoding='cp437', errors='replace')
429 self.ann_field(offset, offset+17, 'Model name: %s' % text.strip())
431 # Monitor range limits
432 self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
433 [ANN_SECTIONS, ['Monitor range limits']])
434 self.ann_field(offset+5, offset+5, 'Minimum vertical rate: %dHz' %
435 self.cache[offset+5])
436 self.ann_field(offset+6, offset+6, 'Maximum vertical rate: %dHz' %
437 self.cache[offset+6])
438 self.ann_field(offset+7, offset+7, 'Minimum horizontal rate: %dkHz' %
439 self.cache[offset+7])
440 self.ann_field(offset+8, offset+8, 'Maximum horizontal rate: %dkHz' %
441 self.cache[offset+8])
442 self.ann_field(offset+9, offset+9, 'Maximum pixel clock: %dMHz' %
443 (self.cache[offset+9] * 10))
444 if self.cache[offset+10] == 0x02:
445 # Secondary GTF curve supported
446 self.ann_field(offset+10, offset+17, 'Secondary timing formula supported')
448 # Additional color point data
449 self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
450 [ANN_SECTIONS, ['Additional color point data']])
452 # Additional standard timing definitions
453 self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
454 [ANN_SECTIONS, ['Additional standard timing definitions']])
456 self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
457 [ANN_SECTIONS, ['Unknown descriptor']])
459 def decode_descriptors(self, offset):
460 # 4 consecutive 18-byte descriptor blocks
461 for i in range(offset, 0, 18):
462 if self.cache[i] != 0 and self.cache[i+1] != 0:
463 self.decode_detailed_timing(i)
465 if self.cache[i+2] == 0 or self.cache[i+4] == 0:
466 self.decode_descriptor(i)