File Coverage

blib/lib/Parse/EDID.pm
Criterion Covered Total %
statement 225 231 97.4
branch 130 156 83.3
condition 38 69 55.0
subroutine 18 18 100.0
pod 3 3 100.0
total 414 477 86.7


line stmt bran cond sub pod time code
1             # Copyright (C) 2005-2010 by Mandriva SA
2             # Pascal Rigaux
3             # Anssi Hannula
4             #
5             # This program is free software; you can redistribute it and/or modify
6             # it under the terms of the GNU General Public License as published by
7             # the Free Software Foundation; either version 3, or (at your option)
8             # any later version.
9             #
10             # This program is distributed in the hope that it will be useful,
11             # but WITHOUT ANY WARRANTY; without even the implied warranty of
12             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13             # GNU General Public License for more details.
14             #
15             # You should have received a copy of the GNU General Public License
16             # along with this program. If not, see .
17              
18             package Parse::EDID;
19              
20 2     2   214123 use strict;
  2         8  
  2         117  
21 2     2   12 use warnings;
  2         3  
  2         77  
22 2     2   13 use base 'Exporter';
  2         9  
  2         21585  
23              
24             our $VERSION = '1.0.6';
25              
26             our @EXPORT = qw(
27             parse_edid
28             check_parsed_edid
29             find_edid_in_string
30             );
31              
32             my @CVT_ratios = qw(5/4 4/3 3/2 16/10 15/9 16/9);
33             my @known_ratios = @CVT_ratios;
34              
35             my @edid_info = _group_by2(
36             a8 => '_header',
37             a2 => 'manufacturer_name',
38              
39             v => 'product_code',
40             V => 'serial_number',
41             C => 'week',
42             C => 'year',
43             C => 'edid_version',
44             C => 'edid_revision',
45             a => 'video_input_definition',
46              
47             C => 'max_size_horizontal', # in cm, 0 on projectors
48             C => 'max_size_vertical', # in cm, 0 on projectors
49             C => 'gamma',
50             a => 'feature_support',
51             a10 => '_color_characteristics',
52             a3 => 'established_timings',
53             a16 => 'standard_timings',
54             a72 => 'monitor_details',
55              
56             C => 'extension_flag',
57             C => 'checksum',
58             );
59              
60             my %subfields = (
61             manufacturer_name => [ _group_by2(
62             1 => '',
63             5 => '1',
64             5 => '2',
65             5 => '3',
66             ) ],
67              
68             video_input_definition => [ _group_by2(
69             1 => 'digital',
70             1 => 'separate_sync',
71             1 => 'composite_sync',
72             1 => 'sync_on_green',
73             2 => '',
74             2 => 'voltage_level',
75             ) ],
76              
77             feature_support => [ _group_by2(
78             1 => 'DPMS_standby',
79             1 => 'DPMS_suspend',
80             1 => 'DPMS_active_off',
81             1 => 'rgb',
82              
83             1 => '',
84             1 => 'sRGB_compliance',
85             1 => 'has_preferred_timing',
86             1 => 'GTF_compliance',
87             ) ],
88              
89             established_timings => [ _group_by2(
90             1 => '720x400_70',
91             1 => '720x400_88',
92             1 => '640x480_60',
93             1 => '640x480_67',
94             1 => '640x480_72',
95             1 => '640x480_75',
96             1 => '800x600_56',
97             1 => '800x600_60',
98             1 => '800x600_72',
99             1 => '800x600_75',
100             1 => '832x624_75',
101             1 => '1024x768_87i',
102             1 => '1024x768_60',
103             1 => '1024x768_70',
104             1 => '1024x768_75',
105             1 => '1280x1024_75',
106             ) ],
107              
108             detailed_timing => [ _group_by2(
109             8 => 'horizontal_active',
110             8 => 'horizontal_blanking',
111             4 => 'horizontal_active_hi',
112             4 => 'horizontal_blanking_hi',
113             8 => 'vertical_active',
114             8 => 'vertical_blanking',
115             4 => 'vertical_active_hi',
116             4 => 'vertical_blanking_hi',
117             8 => 'horizontal_sync_offset',
118             8 => 'horizontal_sync_pulse_width',
119             4 => 'vertical_sync_offset',
120             4 => 'vertical_sync_pulse_width',
121             2 => 'horizontal_sync_offset_hi',
122             2 => 'horizontal_sync_pulse_width_hi',
123             2 => 'vertical_sync_offset_hi',
124             2 => 'vertical_sync_pulse_width_hi',
125             8 => 'horizontal_image_size', # in mm
126             8 => 'vertical_image_size', # in mm
127             4 => 'horizontal_image_size_hi',
128             4 => 'vertical_image_size_hi',
129             8 => 'horizontal_border',
130             8 => 'vertical_border',
131              
132             1 => 'interlaced',
133             2 => 'stereo',
134             2 => 'digital_composite',
135             1 => 'horizontal_sync_positive',
136             1 => 'vertical_sync_positive',
137             1 => '',
138             ) ],
139              
140             standard_timing => [ _group_by2(
141             8 => 'X',
142             2 => 'aspect',
143             6 => 'vfreq',
144             ) ],
145             monitor_range => [ _group_by2(
146             8 => 'vertical_min',
147             8 => 'vertical_max',
148             8 => 'horizontal_min',
149             8 => 'horizontal_max',
150             8 => 'pixel_clock_max',
151             ) ],
152              
153             manufacturer_specified_range_timing => [ _group_by2(
154             # http://www.spwg.org/salisbury_march_19_2002.pdf
155             # for the glossary: http://www.vesa.org/Public/PSWG/PSWG15v1.pdf
156             8 => 'horizontal_sync_pulse_width_min', # HSPW (Horizontal Sync Pulse Width)
157             8 => 'horizontal_sync_pulse_width_max',
158             8 => 'horizontal_back_porch_min', # t_hbp
159             8 => 'horizontal_back_porch_max',
160             8 => 'vertical_sync_pulse_width_min', # VSPW (Vertical Sync Pulse Width)
161             8 => 'vertical_sync_pulse_width_max',
162             8 => 'vertical_back_porch_min', # t_vbp (Vertical Back Porch)
163             8 => 'vertical_back_porch_max',
164             8 => 'horizontal_blanking_min', # t_hp (Horizontal Period)
165             8 => 'horizontal_blanking_max',
166             8 => 'vertical_blanking_min', # t_vp
167             8 => 'vertical_blanking_max',
168             8 => 'module_revision',
169             ) ],
170            
171             cea_data_block_collection => [ _group_by2(
172             3 => 'type',
173             5 => 'size',
174             ) ],
175              
176             cea_video_data_block => [ _group_by2(
177             1 => 'native',
178             7 => 'mode',
179             ) ],
180             );
181              
182             my @cea_video_mode_to_detailed_timing = (
183             'pixel_clock',
184             'horizontal_active',
185             'vertical_active',
186             'aspect',
187             'horizontal_blanking',
188             'horizontal_sync_offset',
189             'horizontal_sync_pulse_width',
190             'vertical_blanking',
191             'vertical_sync_offset',
192             'vertical_sync_pulse_width',
193             'horizontal_sync_positive',
194             'vertical_sync_positive',
195             'interlaced'
196             );
197              
198             my @cea_video_modes = (
199             # [0] pixel clock, [1] X, [2] Y, [3] aspect, [4] Hblank, [5] Hsync_offset, [6] Hsync_pulse_width,
200             # [7] Vblank, [8] Vsync_offset, [9] Vsync_pulse_width, [10] Hsync+, [11] Vsync+, [12] interlaced
201             # 59.94/29.97 and similar modes also have a 60.00/30.00 counterpart by raising the pixel clock
202             [ 25.175, 640, 480, "4/3", 160, 16, 96, 45, 10, 2, 0, 0, 0 ], # 1: 640x 480@59.94
203             [ 27.000, 720, 480, "4/3", 138, 16, 62, 45, 9, 6, 0, 0, 0 ], # 2: 720x 480@59.94
204             [ 27.000, 720, 480, "16/9", 138, 16, 62, 45, 9, 6, 0, 0, 0 ], # 3: 720x 480@59.94
205             [ 74.250, 1280, 720, "16/9", 370, 110, 40, 30, 5, 5, 1, 1, 0 ], # 4: 1280x 720@60.00
206             [ 74.250, 1920, 1080, "16/9", 280, 88, 44, 45, 4, 10, 1, 1, 1 ], # 5: 1920x1080@30.00
207             [ 27.000, 1440, 480, "4/3", 276, 38, 124, 45, 8, 6, 0, 0, 1 ], # 6: 1440x 480@29.97
208             [ 27.000, 1440, 480, "16/9", 276, 38, 124, 45, 8, 6, 0, 0, 1 ], # 7: 1440x 480@29.97
209             [ 27.000, 1440, 240, "4/3", 276, 38, 124, 22, 4, 3, 0, 0, 0 ], # 8: 1440x 240@60.05
210             [ 27.000, 1440, 240, "16/9", 276, 38, 124, 22, 4, 3, 0, 0, 0 ], # 9: 1440x 240@60.05
211             [ 54.000, 2880, 480, "4/3", 552, 76, 248, 45, 8, 6, 0, 0, 1 ], # 10: 2880x 480@29.97
212             [ 54.000, 2880, 480, "16/9", 552, 76, 248, 45, 8, 6, 0, 0, 1 ], # 11: 2880x 480@29.97
213             [ 54.000, 2880, 240, "4/3", 552, 76, 248, 22, 4, 3, 0, 0, 0 ], # 12: 2880x 240@60.05
214             [ 54.000, 2880, 240, "16/9", 552, 76, 248, 22, 4, 3, 0, 0, 0 ], # 13: 2880x 240@60.05
215             [ 54.000, 1440, 480, "4/3", 276, 32, 124, 45, 9, 6, 0, 0, 0 ], # 14: 1440x 480@59.94
216             [ 54.000, 1440, 480, "16/9", 276, 32, 124, 45, 9, 6, 0, 0, 0 ], # 15: 1440x 480@59.94
217             [ 148.500, 1920, 1080, "16/9", 280, 88, 44, 45, 4, 5, 1, 1, 0 ], # 16: 1920x1080@60.00
218             [ 27.000, 720, 576, "4/3", 144, 12, 64, 49, 5, 5, 0, 0, 0 ], # 17: 720x 576@50.00
219             [ 27.000, 720, 576, "16/9", 144, 12, 64, 49, 5, 5, 0, 0, 0 ], # 18: 720x 576@50.00
220             [ 74.250, 1280, 720, "16/9", 700, 440, 40, 30, 5, 5, 1, 1, 0 ], # 19: 1280x 720@50.00
221             [ 74.250, 1920, 1080, "16/9", 720, 528, 44, 45, 4, 10, 1, 1, 1 ], # 20: 1920x1080@25.00
222             [ 27.000, 1440, 576, "4/3", 288, 24, 126, 49, 4, 6, 0, 0, 1 ], # 21: 1440x 576@25.00
223             [ 27.000, 1440, 576, "16/9", 288, 24, 126, 49, 4, 6, 0, 0, 1 ], # 22: 1440x 576@25.00
224             [ 27.000, 1440, 288, "4/3", 288, 24, 126, 24, 2, 3, 0, 0, 0 ], # 23: 1440x 288@50.08
225             [ 27.000, 1440, 288, "16/9", 288, 24, 126, 24, 2, 3, 0, 0, 0 ], # 24: 1440x 288@50.08
226             [ 54.000, 2880, 576, "4/3", 576, 48, 252, 49, 4, 6, 0, 0, 1 ], # 25: 2880x 576@25.00
227             [ 54.000, 2880, 576, "16/9", 576, 48, 252, 49, 4, 6, 0, 0, 1 ], # 26: 2880x 576@25.00
228             [ 54.000, 2880, 288, "4/3", 576, 48, 252, 24, 2, 3, 0, 0, 0 ], # 27: 2880x 288@50.08
229             [ 54.000, 2880, 288, "16/9", 576, 48, 252, 24, 2, 3, 0, 0, 0 ], # 28: 2880x 288@50.08
230             [ 54.000, 1440, 576, "4/3", 288, 24, 128, 49, 5, 5, 0, 0, 0 ], # 29: 1440x 576@50.00
231             [ 54.000, 1440, 576, "16/9", 288, 24, 128, 49, 5, 5, 0, 0, 0 ], # 30: 1440x 576@50.00
232             [ 148.500, 1920, 1080, "16/9", 720, 528, 44, 45, 4, 5, 1, 1, 0 ], # 31: 1920x1080@50.00
233             [ 74.250, 1920, 1080, "16/9", 830, 638, 44, 45, 4, 5, 1, 1, 0 ], # 32: 1920x1080@24.00
234             [ 74.250, 1920, 1080, "16/9", 720, 528, 44, 45, 4, 5, 1, 1, 0 ], # 33: 1920x1080@25.00
235             [ 74.250, 1920, 1080, "16/9", 280, 88, 44, 45, 4, 5, 1, 1, 0 ], # 34: 1920x1080@30.00
236             [ 108.000, 2880, 480, "4/3", 552, 64, 248, 45, 9, 6, 0, 0, 0 ], # 35: 2880x 480@59.94
237             [ 108.000, 2880, 480, "16/9", 552, 64, 248, 45, 9, 6, 0, 0, 0 ], # 36: 2880x 480@59.94
238             [ 108.000, 2880, 576, "4/3", 576, 48, 256, 49, 5, 5, 0, 0, 0 ], # 37: 2880x 576@50.00
239             [ 108.000, 2880, 576, "16/9", 576, 48, 256, 49, 5, 5, 0, 0, 0 ], # 38: 2880x 576@50.00
240             [ 72.000, 1920, 1080, "16/9", 384, 32, 168, 170, 46, 10, 1, 0, 1 ], # 39: 1920x1080@25.00
241             [ 148.500, 1920, 1080, "16/9", 720, 528, 44, 45, 4, 10, 1, 1, 1 ], # 40: 1920x1080@50.00
242             [ 148.500, 1280, 720, "16/9", 700, 440, 40, 30, 5, 5, 1, 1, 0 ], # 41: 1280x 720@100.00
243             [ 54.000, 720, 576, "4/3", 144, 12, 64, 49, 5, 5, 0, 0, 0 ], # 42: 720x 576@100.00
244             [ 54.000, 720, 576, "16/9", 144, 12, 64, 49, 5, 5, 0, 0, 0 ], # 43: 720x 576@100.00
245             [ 54.000, 1440, 576, "4/3", 288, 24, 126, 49, 4, 6, 0, 0, 0 ], # 44: 1440x 576@50.00
246             [ 54.000, 1440, 576, "16/9", 288, 24, 126, 49, 4, 6, 0, 0, 0 ], # 45: 1440x 576@50.00
247             [ 148.500, 1920, 1080, "16/9", 280, 88, 44, 45, 4, 10, 1, 1, 1 ], # 46: 1920x1080@60.00
248             [ 148.500, 1280, 720, "16/9", 370, 110, 40, 30, 5, 5, 1, 1, 0 ], # 47: 1280x 720@120.00
249             [ 54.000, 720, 480, "4/3", 138, 16, 62, 45, 9, 6, 0, 0, 0 ], # 48: 720x 480@119.88
250             [ 54.000, 720, 480, "16/9", 138, 16, 62, 45, 9, 6, 0, 0, 0 ], # 49: 720x 480@119.88
251             [ 54.000, 1440, 480, "4/3", 276, 38, 124, 45, 8, 6, 0, 0, 1 ], # 50: 1440x 480@59.94
252             [ 54.000, 1440, 480, "16/9", 276, 38, 124, 45, 8, 6, 0, 0, 1 ], # 51: 1440x 480@59.94
253             [ 108.000, 720, 576, "4/3", 144, 12, 64, 49, 5, 5, 0, 0, 0 ], # 52: 720x 576@200.00
254             [ 108.000, 720, 576, "16/9", 144, 12, 64, 49, 5, 5, 0, 0, 0 ], # 53: 720x 576@200.00
255             [ 108.000, 1440, 576, "4/3", 288, 24, 126, 49, 4, 6, 0, 0, 1 ], # 54: 1440x 576@100.00
256             [ 108.000, 1440, 576, "16/9", 288, 24, 126, 49, 4, 6, 0, 0, 1 ], # 55: 1440x 576@100.00
257             [ 108.000, 720, 480, "4/3", 138, 16, 62, 45, 9, 6, 0, 0, 0 ], # 56: 720x 480@239.76
258             [ 108.000, 720, 480, "16/9", 138, 16, 62, 45, 9, 6, 0, 0, 0 ], # 57: 720x 480@239.76
259             [ 108.000, 1440, 480, "4/3", 276, 38, 124, 45, 8, 6, 0, 0, 1 ], # 58: 1440x 480@119.88
260             [ 108.000, 1440, 480, "16/9", 276, 38, 124, 45, 8, 6, 0, 0, 1 ], # 59: 1440x 480@119.88
261             [ 59.400, 1280, 720, "16/9", 2020, 1760, 40, 30, 5, 5, 1, 1, 0 ], # 60: 1280x 720@24.00
262             [ 74.250, 1280, 720, "16/9", 2680, 2420, 40, 30, 5, 5, 1, 1, 0 ], # 61: 1280x 720@25.00
263             [ 74.250, 1280, 720, "16/9", 2020, 1760, 40, 30, 5, 5, 1, 1, 0 ], # 62: 1280x 720@30.00
264             [ 297.000, 1920, 1080, "16/9", 280, 88, 44, 45, 4, 5, 1, 1, 0 ], # 63: 1920x1080@120.00
265             [ 297.000, 1920, 1080, "16/9", 720, 528, 44, 45, 4, 10, 1, 1, 0 ], # 64: 1920x1080@100.00
266             );
267              
268             sub _within_limit {
269 66     66   123 my ($value, $type, $limit) = @_;
270 66 100       506 $type eq 'min' ? $value >= $limit : $value <= $limit;
271             }
272              
273             sub _get_many_bits {
274 835     835   8541 my ($s, $field_name) = @_;
275 835         54215 my @bits = split('', unpack('B*', $s));
276 835         3227 my %h;
277 835         13727 foreach (@{$subfields{$field_name}}) {
  835         29329  
278 5839         21739 my ($size, $field) = @$_;
279 5839         42329 my @l = ('0' x (8 - $size), splice(@bits, 0, $size));
280 5839 100 66     134178 $h{$field} = unpack("C", pack('B*', join('', @l))) if $field && $field !~ /^_/;
281             }
282 835         3329 \%h;
283             }
284              
285             sub check_parsed_edid {
286 57     57 1 334 my ($edid) = @_;
287              
288 57 50 33     598 $edid->{edid_version} >= 1 && $edid->{edid_version} <= 2 or return 'bad edid_version';
289 57 50       206 $edid->{edid_revision} != 0xff or return 'bad edid_revision';
290              
291 57 100       190 if ($edid->{monitor_range}) {
292 43 50 33     344 $edid->{monitor_range}{horizontal_min} &&
293             $edid->{monitor_range}{horizontal_min} <= $edid->{monitor_range}{horizontal_max}
294             or return 'bad HorizSync';
295 43 50 33     293 $edid->{monitor_range}{vertical_min} &&
296             $edid->{monitor_range}{vertical_min} <= $edid->{monitor_range}{vertical_max}
297             or return 'bad VertRefresh';
298             }
299              
300 57         577 '';
301             }
302              
303             sub _build_detailed_timing {
304 78     78   133 my ($pixel_clock, $vv) = @_;
305              
306 78         170 my $h = _get_many_bits($vv, 'detailed_timing');
307 78         286 $h->{pixel_clock} = $pixel_clock / 100; # to have it in MHz
308 78         140 my %detailed_timing_field_size = map { $_->[1], $_->[0] } @{$subfields{detailed_timing}};
  2184         24793  
  78         298  
309 78         1627 foreach my $field (keys %detailed_timing_field_size) {
310 2184 100       23871 $field =~ s/_hi$// or next;
311 780         2221 my $hi = delete($h->{$field . '_hi'});
312 780         6077 $h->{$field} += $hi << $detailed_timing_field_size{$field};
313             }
314 78         1829 $h;
315             }
316              
317             sub _add_standard_timing_modes {
318 57     57   101 my ($edid, $v) = @_;
319              
320 57 100 66     1181 my @aspect2ratio = (
321             $edid->{edid_version} > 1 || $edid->{edid_revision} > 2 ? '16/10' : '1/1',
322             '4/3', '5/4', '16/9',
323             );
324 456         1176 $v = [ map {
325 57         459 my $h = _get_many_bits($_, 'standard_timing');
326 456         1991 $h->{X} = ($h->{X} + 31) * 8;
327 456 100 66     3898 if ($_ ne "\x20\x20" && $h->{X} > 256) { # cf VALID_TIMING in Xorg edid.h
328 212         326 $h->{vfreq} += 60;
329 212 50       831 if ($h->{ratio} = $aspect2ratio[$h->{aspect}]) {
330 212         423 delete $h->{aspect};
331 212         17231 $h->{Y} = $h->{X} / eval($h->{ratio});
332             }
333 212         1057 $h;
334 244         661 } else { () }
335             } unpack('a2' x (length($v) / 2), $v) ];
336 57         705 $v;
337             }
338              
339             sub parse_edid {
340 57     57 1 1130799 my ($raw_edid, $verbose) = @_;
341              
342 57         162 my %edid;
343 57         681 my ($main_edid, @eedid_blocks) = unpack("a128" x (length($raw_edid) / 128), $raw_edid);
344 57         218 my @vals = unpack(join('', map { $_->[0] } @edid_info), $main_edid);
  1083         4822  
345 57         215 my $i;
346 57         194 foreach (@edid_info) {
347 1083         25319 my ($field, $v) = ($_->[1], $vals[$i++]);
348              
349 1083 100       6186 if ($field eq 'year') {
    100          
    100          
    100          
    100          
    100          
    100          
350 57         152 $v += 1990;
351             } elsif ($field eq 'manufacturer_name') {
352 57         318 my $h = _get_many_bits($v, 'manufacturer_name');
353 57         204 $v = join('', map { chr(ord('A') + $h->{$_} - 1) } 1 .. 3);
  171         671  
354 57 50       310 $v = "" if $v eq "@@@";
355             } elsif ($field eq 'video_input_definition') {
356 57         148 $v = _get_many_bits($v, 'video_input_definition');
357             } elsif ($field eq 'feature_support') {
358 57         2288 $v = _get_many_bits($v, 'feature_support');
359             } elsif ($field eq 'established_timings') {
360 57         1176 my $h = _get_many_bits($v, 'established_timings');
361 1407 50       4821 $v = [
362 539 100       6637 sort { $a->{X} <=> $b->{X} || $a->{vfreq} <=> $b->{vfreq} }
    50          
363 912         1584 map { /(\d+)x(\d+)_(\d+)(i?)/ ? { X => $1, Y => $2, vfreq => $3, $4 ? (interlace => 1) : () } : () }
364 57         401 grep { $h->{$_} } keys %$h ];
365             } elsif ($field eq 'standard_timings') {
366 57         224 $v = _add_standard_timing_modes(\%edid, $v);
367             } elsif ($field eq 'monitor_details') {
368 57         163 while ($v) {
369 228         1214 (my $pixel_clock, my $vv, $v) = unpack("v a16 a*", $v);
370              
371 228 100       636 if ($pixel_clock) {
372             # detailed timing
373 69         236 my $h = _build_detailed_timing($pixel_clock, $vv);
374 69 100 66     552 push @{$edid{detailed_timings}}, $h
  65         344  
375             if $h->{horizontal_active} > 1 && $h->{vertical_active} > 1;
376             } else {
377 159         870 (my $flag, $vv) = unpack("n x a*", $vv);
378              
379 159 100       843 if ($flag == 0xfd) {
    100          
    50          
    100          
    100          
    100          
380             # range
381 42         121 $edid{monitor_range} = _get_many_bits($vv, 'monitor_range');
382 42 100       265 if ($edid{monitor_range}{pixel_clock_max} == 0xff) {
383 5         24 delete $edid{monitor_range}{pixel_clock_max};
384             } else {
385 37         161 $edid{monitor_range}{pixel_clock_max} *= 10; #- to have it in MHz
386             }
387             } elsif ($flag == 0xf) {
388 7         27 my $range = _get_many_bits($vv, 'manufacturer_specified_range_timing');
389              
390 7         28 my $e = $edid{detailed_timings}[0];
391 7         16 my $valid = 1;
392 7         22 foreach my $m ('min', 'max') {
393 14         23 my %total;
394 14         30 foreach my $dir ('horizontal', 'vertical') {
395 28         79 $range->{$dir . '_sync_pulse_width_' . $m} *= 2;
396 28         59 $range->{$dir . '_back_porch_' . $m} *= 2;
397 28         59 $range->{$dir . '_blanking_' . $m} *= 2;
398 28 100 33     337 if ($e && $e->{$dir . '_active'}
      66        
      100        
      100        
399             && _within_limit($e->{$dir . '_blanking'}, $m, $range->{$dir . '_blanking_' . $m})
400             && _within_limit($e->{$dir . '_sync_pulse_width'}, $m, $range->{$dir . '_sync_pulse_width_' . $m})
401             && _within_limit($e->{$dir . '_blanking'} - $e->{$dir . '_sync_offset'} - $e->{$dir . '_sync_pulse_width'},
402             $m, $range->{$dir . '_back_porch_' . $m})) {
403 13         82 $total{$dir} = $e->{$dir . '_active'} + $range->{$dir . '_blanking_' . $m};
404             }
405             }
406 14 100 66     74 if ($total{horizontal} && $total{vertical}) {
407 5         25 my $hfreq = $e->{pixel_clock} * 1000 / $total{horizontal};
408 5         16 my $vfreq = $hfreq * 1000 / $total{vertical};
409 5 100       28 $range->{'horizontal_' . ($m eq 'min' ? 'max' : 'min')} = _round($hfreq);
410 5 100       13 $range->{'vertical_' . ($m eq 'min' ? 'max' : 'min')} = _round($vfreq);
411             } else {
412 9         25 $valid = 0;
413             }
414             }
415 7 100       71 $edid{$valid ? 'monitor_range' : 'manufacturer_specified_range_timing'} = $range;
416              
417             } elsif ($flag == 0xfa) {
418 0         0 push @{$edid{standard_timings}}, _add_standard_timing_modes(\%edid, unpack('a12', $vv));
  0         0  
419             } elsif ($flag == 0xfc) {
420 40         98 my $prev = $edid{monitor_name};
421 40 100       292 $edid{monitor_name} = ($prev ? "$prev " : '') . unpack('A13', $vv);
422             } elsif ($flag == 0xfe) {
423 28         51 push @{$edid{monitor_text}}, unpack('A13', $vv);
  28         149  
424             } elsif ($flag == 0xff) {
425 33         55 push @{$edid{serial_number2}}, unpack('A13', $vv);
  33         277  
426             } else {
427 9 0 33     45 $verbose && $vv ne "\0" x 13 && $vv ne " " x 13 and
      33        
428             warn "parse_edid: unknown flag $flag\n";
429             }
430             }
431             }
432             }
433              
434 1083 100 66     22795 $edid{$field} = $v if $field && $field !~ /^_/;
435             }
436              
437 57         217 foreach (@eedid_blocks) {
438 3         17 my ($tag, $v) = unpack("C a*", $_);
439              
440 3 100       95 if ($tag == 0x02) { # CEA EDID
441 2         6 my $dtd_offset;
442 2         10 ($dtd_offset, $v) = unpack("x C x a*", $v);
443              
444 2 50       23 next if $dtd_offset < 4;
445 2         5 $dtd_offset -= 4;
446              
447 2         10 while ($dtd_offset > 0) {
448 5         12 my $h = _get_many_bits($v, 'cea_data_block_collection');
449 5         16 $dtd_offset -= $h->{size} + 1;
450              
451 5         80 my $vv;
452 5         66 ($vv, $v) = unpack("x a$h->{size} a*", $v);
453 5 100       25 if ($h->{type} == 0x02) { # Video Data Block
454 2         15 my @vmodes = unpack("a" x $h->{size}, $vv);
455 2         8 foreach my $vmode (@vmodes) {
456 19         43 $h = _get_many_bits($vmode, 'cea_video_data_block');
457 19         165 my $cea_mode = $cea_video_modes[$h->{mode} - 1];
458 19 50       46 if (!$cea_mode) {
459 0 0       0 warn "parse_edid: unhandled CEA mode $h->{mode}\n" if $verbose;
460 0         0 next;
461             }
462 19         46 my %det_mode = (source => 'cea_vdb');
463 19         186 @det_mode{@cea_video_mode_to_detailed_timing} = @$cea_mode;
464 19         35 push @{$edid{detailed_timings}}, \%det_mode;
  19         76  
465             }
466             }
467             }
468              
469 2         28 while (length($v) >= 18) {
470 10         51 (my $pixel_clock, my $vv, $v) = unpack("v a16 a*", $v);
471 10 100       34 last if !$pixel_clock;
472 9         27 my $h = _build_detailed_timing($pixel_clock, $vv);
473 9 50 33     65 push @{$edid{detailed_timings}}, $h
  9         52  
474             if $h->{horizontal_active} > 1 && $h->{vertical_active} > 1;
475             }
476             } else {
477 1 50       4 $verbose && warn "parse_edid: unknown tag $tag\n";
478             }
479             }
480              
481 57         194 $edid{max_size_precision} = 'cm';
482 57 100 66     1217 $edid{EISA_ID} = $edid{manufacturer_name} . sprintf('%04x', $edid{product_code}) if $edid{product_code} && $edid{manufacturer_name};
483              
484 57 100       280 if ($edid{monitor_range}) {
485 43         224 $edid{HorizSync} = $edid{monitor_range}{horizontal_min} . '-' . $edid{monitor_range}{horizontal_max};
486 43         217 $edid{VertRefresh} = $edid{monitor_range}{vertical_min} . '-' . $edid{monitor_range}{vertical_max};
487             }
488              
489 57 50       189 if ($edid{max_size_vertical}) {
490 57         275 $edid{ratio} = $edid{max_size_horizontal} / $edid{max_size_vertical};
491 57         289 $edid{ratio_name} = _ratio_name($edid{max_size_horizontal}, $edid{max_size_vertical}, 'cm');
492 57         203 $edid{ratio_precision} = 'cm';
493             }
494            
495 57 100 66     1130 if ($edid{feature_support}{has_preferred_timing} && $edid{detailed_timings}[0]) {
496 39         171 $edid{detailed_timings}[0]{preferred} = 1;
497             }
498              
499 57         118 foreach my $h (@{$edid{detailed_timings}}) {
  57         177  
500              
501             # EDID standard is ambiguous on how interlaced modes should be
502             # specified; workaround clearly broken modes:
503 93 100       289 if ($h->{interlaced}) {
504 6         16 foreach ("720x480", "1440x480", "2880x480", "720x576", "1440x576", "2880x576", "1920x1080") {
505 42 100       1293 if ($_ eq $h->{horizontal_active} . 'x' . $h->{vertical_active} * 2) {
506 2         5 $h->{vertical_active} *= 2;
507 2         5 $h->{vertical_blanking} *= 2;
508 2         4 $h->{vertical_sync_offset} *= 2;
509 2         7 $h->{vertical_sync_pulse_width} *= 2;
510 2         7 $h->{vertical_blanking} |= 1;
511             }
512             }
513             }
514              
515             # if the mm size given in the detailed_timing is not far from the cm size
516             # put it as a more precise cm size
517 93         398 my %in_cm = (
518             horizontal => _define($h->{horizontal_image_size}) / 10,
519             vertical => _define($h->{vertical_image_size}) / 10,
520             );
521 93         407 my ($error) = sort { $b <=> $a } map { abs($edid{'max_size_' . $_} - $in_cm{$_}) } keys %in_cm;
  93         254  
  186         969  
522 93 100       335 if ($error <= 0.5) {
523 48         306 $edid{'max_size_' . $_} = $in_cm{$_} foreach keys %in_cm;
524 48         296 $edid{max_size_precision} = 'mm';
525             }
526 93 100 66     970 if ($error < 1 && $in_cm{vertical}) {
527             # using it for the ratio
528 63         162 $edid{ratio} = $in_cm{horizontal} / $in_cm{vertical};
529 63         198 $edid{ratio_name} = _ratio_name($in_cm{horizontal}, $in_cm{vertical}, 'mm');
530 63         231 $edid{ratio_precision} = 'mm';
531             }
532              
533 93 100       1065 $h->{bad_ratio} = 1 if abs($edid{ratio} - $h->{horizontal_active} / $h->{vertical_active}) > ($edid{ratio_precision} eq 'mm' ? 0.02 : 0.2);
    100          
534              
535 93 50       351 if ($edid{max_size_vertical}) {
536 93         428 $h->{vertical_dpi} = $h->{vertical_active} / $edid{max_size_vertical} * 2.54;
537             }
538 93 50       293 if ($edid{max_size_horizontal}) {
539 93         403 $h->{horizontal_dpi} = $h->{horizontal_active} / $edid{max_size_horizontal} * 2.54;
540             }
541 93         153 my $dpi_string = '';
542 93 50 33     507 if ($h->{vertical_dpi} && $h->{horizontal_dpi}) {
543 93 100       589 $dpi_string =
544             abs($h->{vertical_dpi} / $h->{horizontal_dpi} - 1) < 0.05 ?
545             sprintf("%d dpi", $h->{horizontal_dpi}) :
546             sprintf("%dx%d dpi", $h->{horizontal_dpi}, $h->{vertical_dpi});
547             }
548              
549 93         220 my $horizontal_total = $h->{horizontal_active} + $h->{horizontal_blanking};
550 93         342 my $vertical_total = $h->{vertical_active} + $h->{vertical_blanking};
551              
552 2     2   29 no warnings 'uninitialized';
  2         6  
  2         10463  
553 93 100 66     907 $h->{ModeLine_comment} = sprintf qq(# Monitor %s%s modeline (%.1f Hz vsync, %.1f kHz hsync, %sratio %s%s)),
    100          
    100          
    100          
    50          
554             $h->{preferred} ? "preferred" : "supported",
555             $h->{source} eq 'cea_vdb' ? " CEA" : '',
556             $h->{pixel_clock} / $horizontal_total / $vertical_total * 1000 * 1000 * ($h->{interlaced} ? 2 : 1),
557             $h->{pixel_clock} / $horizontal_total * 1000,
558             $h->{interlaced} ? "interlaced, " : '',
559             _nearest_ratio($h->{horizontal_active} / $h->{vertical_active}, 0.01) || sprintf("%.2f", $h->{horizontal_active} / $h->{vertical_active}),
560             $dpi_string ? ", $dpi_string" : '';
561            
562 93 100       10974 $h->{ModeLine} = sprintf qq("%dx%d" $h->{pixel_clock} %d %d %d %d %d %d %d %d %shsync %svsync%s),
    100          
    100          
563             $h->{horizontal_active}, $h->{vertical_active},
564              
565             $h->{horizontal_active},
566             $h->{horizontal_active} + $h->{horizontal_sync_offset},
567             $h->{horizontal_active} + $h->{horizontal_sync_offset} + $h->{horizontal_sync_pulse_width},
568             $horizontal_total,
569              
570             $h->{vertical_active},
571             $h->{vertical_active} + $h->{vertical_sync_offset},
572             $h->{vertical_active} + $h->{vertical_sync_offset} + $h->{vertical_sync_pulse_width},
573             $vertical_total,
574              
575             $h->{horizontal_sync_positive} ? '+' : '-',
576             $h->{vertical_sync_positive} ? '+' : '-',
577             $h->{interlaced} ? ' Interlace' : '';
578             }
579              
580 57         436 $edid{diagonal_size} = sqrt(_sqr($edid{max_size_horizontal}) +
581             _sqr($edid{max_size_vertical})) / 2.54;
582              
583 57         566 \%edid;
584             }
585              
586             sub _nearest_ratio {
587 315     315   465 my ($ratio, $max_error) = @_;
588 462         1237 my @sorted =
589 1890         195996 sort { $a->[1] <=> $b->[1] }
590             map {
591 315         607 my $error = abs($ratio - eval($_));
592 1890 100       33315 $error > $max_error ? () : [ $_, $error ];
593             } @known_ratios;
594 315         10542 $sorted[0][0];
595             }
596              
597             sub _ratio_name {
598 120     120   269 my ($horizontal, $vertical, $precision) = @_;
599            
600 120 100       416 if ($precision eq 'mm') {
601 63         175 _nearest_ratio($horizontal / $vertical, 0.1);
602             } else {
603 57         106 my $error = 0.5;
604 57         296 my $ratio1 = _nearest_ratio(($horizontal + $error) / ($vertical - $error), 0.2);
605 57         213 my $ratio2 = _nearest_ratio(($horizontal - $error) / ($vertical + $error), 0.2);
606 57 50 33     2055 $ratio1 && $ratio2 or return;
607 57 100       319 if ($ratio1 eq $ratio2) {
608 12         61 $ratio1;
609             } else {
610 45         129 my $ratio = _nearest_ratio($horizontal / $vertical, 0.2);
611 45 100       767 join(' or ', $ratio, $ratio eq $ratio1 ? $ratio2 : $ratio1);
612             }
613             }
614             }
615              
616             sub _edid_from_lines {
617 3     3   9 my (@l) = @_;
618 3 50       7 my $edid_str = join('', map { /\s+([0-9a-f]{32})$/ && $1 } @l);
  24         132  
619 3 50 33     26 if (length($edid_str) % (2 * 128) != 0 || length($edid_str) == 0) {
620 0         0 return ();
621             }
622 3         223 pack("C*", map { hex($_) } $edid_str =~ /(..)/g);
  384         667  
623             }
624              
625             sub find_edid_in_string {
626 2     2 1 2278 my ($input) = @_;
627              
628 2         3 my @edids;
629 2         30 while ($input =~ /(?:EDID_DATA|: EDID \(in hex\)|EDID):\n((.*\n){8})/g) {
630 3         24 push @edids, _edid_from_lines(split '\n', $1);
631             }
632 2 50       5 if (!@edids) {
633 0         0 @edids = _edid_from_lines(split '\n', $input);
634             }
635 2         12 @edids;
636             }
637              
638 186 100   186   1579 sub _define { defined $_[0] ? $_[0] : 0 }
639 114     114   722 sub _sqr { $_[0] * $_[0] }
640 10     10   154 sub _round { int($_[0] + 0.5) }
641             sub _group_by2 {
642 22     22   25 my @l;
643 22         1643 for (my $i = 0; $i < @_; $i += 2) {
644 212         711 push @l, [ $_[$i], $_[$i+1] ];
645             }
646 22         141 @l;
647             }
648              
649             1;
650             __END__