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