File Coverage

blib/lib/Parse/EDID.pm
Criterion Covered Total %
statement 228 234 97.4
branch 135 160 84.3
condition 43 72 59.7
subroutine 18 18 100.0
pod 3 3 100.0
total 427 487 87.6


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   141251 use strict;
  2         18  
  2         51  
21 2     2   11 use warnings;
  2         4  
  2         56  
22 2     2   12 use base 'Exporter';
  2         3  
  2         6175  
23              
24             our $VERSION = '1.0.7';
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   81 my ($value, $type, $limit) = @_;
270 66 100       215 $type eq 'min' ? $value >= $limit : $value <= $limit;
271             }
272              
273             sub _get_many_bits {
274 1072     1072   1304 my ($s, $field_name) = @_;
275 1072         7052 my @bits = split('', unpack('B*', $s));
276 1072         1270 my %h;
277 1072         1250 foreach (@{$subfields{$field_name}}) {
  1072         1614  
278 7007         9017 my ($size, $field) = @$_;
279 7007         9735 my @l = ('0' x (8 - $size), splice(@bits, 0, $size));
280 7007 100 66     26621 $h{$field} = unpack("C", pack('B*', join('', @l))) if $field && $field !~ /^_/;
281             }
282 1072         2931 \%h;
283             }
284              
285             sub check_parsed_edid {
286 61     61 1 17035 my ($edid) = @_;
287              
288 61 50 33     257 $edid->{edid_version} >= 1 && $edid->{edid_version} <= 2 or return 'bad edid_version';
289 61 50       95 $edid->{edid_revision} != 0xff or return 'bad edid_revision';
290              
291 61 100       93 if ($edid->{monitor_range}) {
292             $edid->{monitor_range}{horizontal_min} &&
293             $edid->{monitor_range}{horizontal_min} <= $edid->{monitor_range}{horizontal_max}
294 47 50 33     131 or return 'bad HorizSync';
295             $edid->{monitor_range}{vertical_min} &&
296             $edid->{monitor_range}{vertical_min} <= $edid->{monitor_range}{vertical_max}
297 47 50 33     149 or return 'bad VertRefresh';
298             }
299              
300 61         101 '';
301             }
302              
303             sub _build_detailed_timing {
304 99     99   125 my ($pixel_clock, $vv) = @_;
305              
306 99         110 my $h = _get_many_bits($vv, 'detailed_timing');
307 99         172 $h->{pixel_clock} = $pixel_clock / 100; # to have it in MHz
308 99         114 my %detailed_timing_field_size = map { $_->[1], $_->[0] } @{$subfields{detailed_timing}};
  2772         3224  
  99         150  
309 99         431 foreach my $field (keys %detailed_timing_field_size) {
310 2772 100       4432 $field =~ s/_hi$// or next;
311 990         1476 my $hi = delete($h->{$field . '_hi'});
312 990         1386 $h->{$field} += $hi << $detailed_timing_field_size{$field};
313             }
314 99         420 $h;
315             }
316              
317             sub _add_standard_timing_modes {
318 61     61   85 my ($edid, $v) = @_;
319              
320             my @aspect2ratio = (
321 61 100 66     238 $edid->{edid_version} > 1 || $edid->{edid_revision} > 2 ? '16/10' : '1/1',
322             '4/3', '5/4', '16/9',
323             );
324             $v = [ map {
325 61         246 my $h = _get_many_bits($_, 'standard_timing');
  488         657  
326 488         665 $h->{X} = ($h->{X} + 31) * 8;
327 488 100 66     1040 if ($_ ne "\x20\x20" && $h->{X} > 256) { # cf VALID_TIMING in Xorg edid.h
328 212         222 $h->{vfreq} += 60;
329 212 50       376 if ($h->{ratio} = $aspect2ratio[$h->{aspect}]) {
330 212         262 delete $h->{aspect};
331 212         6258 $h->{Y} = $h->{X} / eval($h->{ratio});
332             }
333 212         506 $h;
334 276         426 } else { () }
335             } unpack('a2' x (length($v) / 2), $v) ];
336 61         153 $v;
337             }
338              
339             sub parse_edid {
340 61     61 1 383215 my ($raw_edid, $verbose) = @_;
341              
342 61         91 my %edid;
343 61         277 my ($main_edid, @eedid_blocks) = unpack("a128" x (length($raw_edid) / 128), $raw_edid);
344 61         111 my @vals = unpack(join('', map { $_->[0] } @edid_info), $main_edid);
  1159         1524  
345 61         122 my $i;
346 61         96 foreach (@edid_info) {
347 1159         1698 my ($field, $v) = ($_->[1], $vals[$i++]);
348              
349 1159 100       2717 if ($field eq 'year') {
    100          
    100          
    100          
    100          
    100          
    100          
350 61         62 $v += 1990;
351             } elsif ($field eq 'manufacturer_name') {
352 61         90 my $h = _get_many_bits($v, 'manufacturer_name');
353 61         93 $v = join('', map { chr(ord('A') + $h->{$_} - 1) } 1 .. 3);
  183         407  
354 61 50       178 $v = "" if $v eq "@@@";
355             } elsif ($field eq 'video_input_definition') {
356 61         86 $v = _get_many_bits($v, 'video_input_definition');
357             } elsif ($field eq 'feature_support') {
358 61         87 $v = _get_many_bits($v, 'feature_support');
359             } elsif ($field eq 'established_timings') {
360 61         79 my $h = _get_many_bits($v, 'established_timings');
361             $v = [
362 1324 50       1836 sort { $a->{X} <=> $b->{X} || $a->{vfreq} <=> $b->{vfreq} }
363 542 100       2451 map { /(\d+)x(\d+)_(\d+)(i?)/ ? { X => $1, Y => $2, vfreq => $3, $4 ? (interlace => 1) : () } : () }
    50          
364 61         220 grep { $h->{$_} } keys %$h ];
  976         1010  
365             } elsif ($field eq 'standard_timings') {
366 61         110 $v = _add_standard_timing_modes(\%edid, $v);
367             } elsif ($field eq 'monitor_details') {
368 61         84 while ($v) {
369 244         608 (my $pixel_clock, my $vv, $v) = unpack("v a16 a*", $v);
370              
371 244 100       351 if ($pixel_clock) {
372             # detailed timing
373 77         129 my $h = _build_detailed_timing($pixel_clock, $vv);
374 73         189 push @{$edid{detailed_timings}}, $h
375 77 100 66     244 if $h->{horizontal_active} > 1 && $h->{vertical_active} > 1;
376             } else {
377 167         263 (my $flag, $vv) = unpack("n x a*", $vv);
378              
379 167 100       380 if ($flag == 0xfd) {
    100          
    50          
    100          
    100          
    100          
380             # range
381 46         74 $edid{monitor_range} = _get_many_bits($vv, 'monitor_range');
382 46 100       88 if ($edid{monitor_range}{pixel_clock_max} == 0xff) {
383 5         11 delete $edid{monitor_range}{pixel_clock_max};
384             } else {
385 41         82 $edid{monitor_range}{pixel_clock_max} *= 10; #- to have it in MHz
386             }
387             } elsif ($flag == 0xf) {
388 7         15 my $range = _get_many_bits($vv, 'manufacturer_specified_range_timing');
389              
390 7         14 my $e = $edid{detailed_timings}[0];
391 7         11 my $valid = 1;
392 7         15 foreach my $m ('min', 'max') {
393 14         17 my %total;
394 14         20 foreach my $dir ('horizontal', 'vertical') {
395 28         46 $range->{$dir . '_sync_pulse_width_' . $m} *= 2;
396 28         39 $range->{$dir . '_back_porch_' . $m} *= 2;
397 28         32 $range->{$dir . '_blanking_' . $m} *= 2;
398 28 100 33     111 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         41 $total{$dir} = $e->{$dir . '_active'} + $range->{$dir . '_blanking_' . $m};
404             }
405             }
406 14 100 66     35 if ($total{horizontal} && $total{vertical}) {
407 5         12 my $hfreq = $e->{pixel_clock} * 1000 / $total{horizontal};
408 5         10 my $vfreq = $hfreq * 1000 / $total{vertical};
409 5 100       9 $range->{'horizontal_' . ($m eq 'min' ? 'max' : 'min')} = _round($hfreq);
410 5 100       10 $range->{'vertical_' . ($m eq 'min' ? 'max' : 'min')} = _round($vfreq);
411             } else {
412 9         15 $valid = 0;
413             }
414             }
415 7 100       30 $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 44         66 my $prev = $edid{monitor_name};
421 44 100       162 $edid{monitor_name} = ($prev ? "$prev " : '') . unpack('A13', $vv);
422             } elsif ($flag == 0xfe) {
423 28         30 push @{$edid{monitor_text}}, unpack('A13', $vv);
  28         75  
424             } elsif ($flag == 0xff) {
425 33         33 push @{$edid{serial_number2}}, unpack('A13', $vv);
  33         119  
426             } else {
427 9 0 33     22 $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 1159 100 66     3471 $edid{$field} = $v if $field && $field !~ /^_/;
435             }
436              
437 61         91 foreach (@eedid_blocks) {
438 7         18 my ($tag, $v) = unpack("C a*", $_);
439              
440 7 100       15 if ($tag == 0x02) { # CEA EDID
441 6         6 my $dtd_offset;
442 6         16 ($dtd_offset, $v) = unpack("x C x a*", $v);
443              
444 6 50       13 next if $dtd_offset < 4;
445 6         8 $dtd_offset -= 4;
446              
447 6         12 while ($dtd_offset > 0) {
448 95 100       123 if (!$v) {
449 1 50       2 warn "parse_edid: DTD offset outside of available data\n" if $verbose;
450 1         2 last;
451             }
452 94         127 my $h = _get_many_bits($v, 'cea_data_block_collection');
453 94         122 $dtd_offset -= $h->{size} + 1;
454              
455 94         85 my $vv;
456 94         221 ($vv, $v) = unpack("x a$h->{size} a*", $v);
457 94 100       214 if ($h->{type} == 0x02) { # Video Data Block
458 6         33 my @vmodes = unpack("a" x $h->{size}, $vv);
459 6         12 foreach my $vmode (@vmodes) {
460 94         136 $h = _get_many_bits($vmode, 'cea_video_data_block');
461 94         125 my $cea_mode = $cea_video_modes[$h->{mode} - 1];
462 94 50       119 if (!$cea_mode) {
463 0 0       0 warn "parse_edid: unhandled CEA mode $h->{mode}\n" if $verbose;
464 0         0 next;
465             }
466 94         129 my %det_mode = (source => 'cea_vdb');
467 94         331 @det_mode{@cea_video_mode_to_detailed_timing} = @$cea_mode;
468 94         83 push @{$edid{detailed_timings}}, \%det_mode;
  94         176  
469             }
470             }
471             }
472              
473 6         17 while (length($v) >= 18) {
474 23         64 (my $pixel_clock, my $vv, $v) = unpack("v a16 a*", $v);
475 23 100       44 last if !$pixel_clock;
476 22         30 my $h = _build_detailed_timing($pixel_clock, $vv);
477 22         57 push @{$edid{detailed_timings}}, $h
478 22 50 33     69 if $h->{horizontal_active} > 1 && $h->{vertical_active} > 1;
479             }
480             } else {
481 1 50       3 $verbose && warn "parse_edid: unknown tag $tag\n";
482             }
483             }
484              
485 61         83 $edid{max_size_precision} = 'cm';
486 61 50 66     288 $edid{EISA_ID} = $edid{manufacturer_name} . sprintf('%04x', $edid{product_code}) if $edid{product_code} && $edid{manufacturer_name};
487              
488 61 100       106 if ($edid{monitor_range}) {
489 47         113 $edid{HorizSync} = $edid{monitor_range}{horizontal_min} . '-' . $edid{monitor_range}{horizontal_max};
490 47         115 $edid{VertRefresh} = $edid{monitor_range}{vertical_min} . '-' . $edid{monitor_range}{vertical_max};
491             }
492              
493 61 100       94 if ($edid{max_size_vertical}) {
494 58         110 $edid{ratio} = $edid{max_size_horizontal} / $edid{max_size_vertical};
495 58         116 $edid{ratio_name} = _ratio_name($edid{max_size_horizontal}, $edid{max_size_vertical}, 'cm');
496 58         96 $edid{ratio_precision} = 'cm';
497             }
498            
499 61 50 66     207 if ($edid{feature_support}{has_preferred_timing} && $edid{detailed_timings}[0]) {
500 43         64 $edid{detailed_timings}[0]{preferred} = 1;
501             }
502              
503 61         76 foreach my $h (@{$edid{detailed_timings}}) {
  61         118  
504              
505             # EDID standard is ambiguous on how interlaced modes should be
506             # specified; workaround clearly broken modes:
507 189 100       335 if ($h->{interlaced}) {
508 39         61 foreach ("720x480", "1440x480", "2880x480", "720x576", "1440x576", "2880x576", "1920x1080") {
509 273 100       420 if ($_ eq $h->{horizontal_active} . 'x' . $h->{vertical_active} * 2) {
510 9         10 $h->{vertical_active} *= 2;
511 9         10 $h->{vertical_blanking} *= 2;
512 9         10 $h->{vertical_sync_offset} *= 2;
513 9         11 $h->{vertical_sync_pulse_width} *= 2;
514 9         10 $h->{vertical_blanking} |= 1;
515             }
516             }
517             }
518              
519             # if the mm size given in the detailed_timing is not far from the cm size
520             # put it as a more precise cm size
521             my %in_cm = (
522             horizontal => _define($h->{horizontal_image_size}) / 10,
523 189         380 vertical => _define($h->{vertical_image_size}) / 10,
524             );
525 189         419 my ($error) = sort { $b <=> $a } map { abs($edid{'max_size_' . $_} - $in_cm{$_}) } keys %in_cm;
  189         368  
  378         905  
526 189 100       330 if ($error <= 0.5) {
527 98         257 $edid{'max_size_' . $_} = $in_cm{$_} foreach keys %in_cm;
528 98         172 $edid{max_size_precision} = 'mm';
529             }
530 189 100 100     422 if ($error < 1 && $in_cm{vertical}) {
531             # using it for the ratio
532 68         114 $edid{ratio} = $in_cm{horizontal} / $in_cm{vertical};
533 68         114 $edid{ratio_name} = _ratio_name($in_cm{horizontal}, $in_cm{vertical}, 'mm');
534 68         102 $edid{ratio_precision} = 'mm';
535             }
536              
537             $h->{bad_ratio} = 1 if
538             $edid{ratio_precision} &&
539 189 100 100     651 abs($edid{ratio} - $h->{horizontal_active} / $h->{vertical_active}) > ($edid{ratio_precision} eq 'mm' ? 0.02 : 0.2);
    100          
540              
541 189 100       288 if ($edid{max_size_vertical}) {
542 128         244 $h->{vertical_dpi} = $h->{vertical_active} / $edid{max_size_vertical} * 2.54;
543             }
544 189 100       250 if ($edid{max_size_horizontal}) {
545 128         192 $h->{horizontal_dpi} = $h->{horizontal_active} / $edid{max_size_horizontal} * 2.54;
546             }
547 189         204 my $dpi_string = '';
548 189 50 66     379 if ($h->{vertical_dpi} && $h->{horizontal_dpi}) {
549             $dpi_string =
550             abs($h->{vertical_dpi} / $h->{horizontal_dpi} - 1) < 0.05 ?
551             sprintf("%d dpi", $h->{horizontal_dpi}) :
552 128 100       433 sprintf("%dx%d dpi", $h->{horizontal_dpi}, $h->{vertical_dpi});
553             }
554              
555 189         260 my $horizontal_total = $h->{horizontal_active} + $h->{horizontal_blanking};
556 189         207 my $vertical_total = $h->{vertical_active} + $h->{vertical_blanking};
557              
558 2     2   16 no warnings 'uninitialized';
  2         5  
  2         2325  
559             $h->{ModeLine_comment} = sprintf qq(# Monitor %s%s modeline (%.1f Hz vsync, %.1f kHz hsync, %sratio %s%s)),
560             $h->{preferred} ? "preferred" : "supported",
561             $h->{source} eq 'cea_vdb' ? " CEA" : '',
562             $h->{pixel_clock} / $horizontal_total / $vertical_total * 1000 * 1000 * ($h->{interlaced} ? 2 : 1),
563             $h->{pixel_clock} / $horizontal_total * 1000,
564             $h->{interlaced} ? "interlaced, " : '',
565 189 100 66     807 _nearest_ratio($h->{horizontal_active} / $h->{vertical_active}, 0.01) || sprintf("%.2f", $h->{horizontal_active} / $h->{vertical_active}),
    100          
    100          
    100          
    100          
566             $dpi_string ? ", $dpi_string" : '';
567            
568             $h->{ModeLine} = sprintf qq("%dx%d" $h->{pixel_clock} %d %d %d %d %d %d %d %d %shsync %svsync%s),
569             $h->{horizontal_active}, $h->{vertical_active},
570              
571             $h->{horizontal_active},
572             $h->{horizontal_active} + $h->{horizontal_sync_offset},
573             $h->{horizontal_active} + $h->{horizontal_sync_offset} + $h->{horizontal_sync_pulse_width},
574             $horizontal_total,
575              
576             $h->{vertical_active},
577             $h->{vertical_active} + $h->{vertical_sync_offset},
578             $h->{vertical_active} + $h->{vertical_sync_offset} + $h->{vertical_sync_pulse_width},
579             $vertical_total,
580              
581             $h->{horizontal_sync_positive} ? '+' : '-',
582             $h->{vertical_sync_positive} ? '+' : '-',
583 189 100       1664 $h->{interlaced} ? ' Interlace' : '';
    100          
    100          
584             }
585              
586             $edid{diagonal_size} = sqrt(_sqr($edid{max_size_horizontal}) +
587 61         131 _sqr($edid{max_size_vertical})) / 2.54;
588              
589 61         303 \%edid;
590             }
591              
592             sub _nearest_ratio {
593 418     418   573 my ($ratio, $max_error) = @_;
594             my @sorted =
595 465         775 sort { $a->[1] <=> $b->[1] }
596             map {
597 418         529 my $error = abs($ratio - eval($_));
  2508         59313  
598 2508 100       6901 $error > $max_error ? () : [ $_, $error ];
599             } @known_ratios;
600 418         2352 $sorted[0][0];
601             }
602              
603             sub _ratio_name {
604 126     126   185 my ($horizontal, $vertical, $precision) = @_;
605            
606 126 100       194 if ($precision eq 'mm') {
607 68         94 _nearest_ratio($horizontal / $vertical, 0.1);
608             } else {
609 58         67 my $error = 0.5;
610 58         116 my $ratio1 = _nearest_ratio(($horizontal + $error) / ($vertical - $error), 0.2);
611 58         189 my $ratio2 = _nearest_ratio(($horizontal - $error) / ($vertical + $error), 0.2);
612 58 50 33     176 $ratio1 && $ratio2 or return;
613 58 100       93 if ($ratio1 eq $ratio2) {
614 13         32 $ratio1;
615             } else {
616 45         89 my $ratio = _nearest_ratio($horizontal / $vertical, 0.2);
617 45 100       166 join(' or ', $ratio, $ratio eq $ratio1 ? $ratio2 : $ratio1);
618             }
619             }
620             }
621              
622             sub _edid_from_lines {
623 3     3   7 my (@l) = @_;
624 3 50       6 my $edid_str = join('', map { /\s+([0-9a-f]{32})$/ && $1 } @l);
  24         86  
625 3 50 33     11 if (length($edid_str) % (2 * 128) != 0 || length($edid_str) == 0) {
626 0         0 return ();
627             }
628 3         102 pack("C*", map { hex($_) } $edid_str =~ /(..)/g);
  384         383  
629             }
630              
631             sub find_edid_in_string {
632 2     2 1 1595 my ($input) = @_;
633              
634 2         3 my @edids;
635 2         19 while ($input =~ /(?:EDID_DATA|: EDID \(in hex\)|EDID):\n((.*\n){8})/g) {
636 3         15 push @edids, _edid_from_lines(split '\n', $1);
637             }
638 2 50       6 if (!@edids) {
639 0         0 @edids = _edid_from_lines(split '\n', $input);
640             }
641 2         5 @edids;
642             }
643              
644 378 100   378   938 sub _define { defined $_[0] ? $_[0] : 0 }
645 122     122   221 sub _sqr { $_[0] * $_[0] }
646 10     10   31 sub _round { int($_[0] + 0.5) }
647             sub _group_by2 {
648 22     22   33 my @l;
649 22         44 for (my $i = 0; $i < @_; $i += 2) {
650 212         534 push @l, [ $_[$i], $_[$i+1] ];
651             }
652 22         90 @l;
653             }
654              
655             1;
656             __END__