1 // SPDX-License-Identifier: MIT
3 * Copyright 2006-2012 Red Hat, Inc.
4 * Copyright 2018-2020 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 * Author: Adam Jackson <ajax@nwnk.net>
7 * Maintainer: Hans Verkuil <hverkuil-cisco@xs4all.nl>
19 #include "edid-decode.h"
22 #define STRING(x) STR(x)
24 static edid_state state
;
26 static unsigned char edid
[EDID_PAGE_SIZE
* EDID_MAX_BLOCKS
];
27 static bool odd_hex_digits
;
39 * Please keep in alphabetical order of the short option.
40 * That makes it easier to see which options are still free.
45 OptFBModeTimings
= 'F',
49 OptNativeResolution
= 'n',
51 OptOutputFormat
= 'o',
52 OptPreferredTimings
= 'p',
53 OptPhysicalAddress
= 'P',
55 OptShortTimings
= 'S',
57 OptXModeLineTimings
= 'X',
79 static char options
[OptLast
];
81 #ifndef __EMSCRIPTEN__
82 static struct option long_options
[] = {
83 { "help", no_argument
, 0, OptHelp
},
84 { "output-format", required_argument
, 0, OptOutputFormat
},
85 { "native-resolution", no_argument
, 0, OptNativeResolution
},
86 { "preferred-timings", no_argument
, 0, OptPreferredTimings
},
87 { "physical-address", no_argument
, 0, OptPhysicalAddress
},
88 { "skip-hex-dump", no_argument
, 0, OptSkipHexDump
},
89 { "only-hex-dump", no_argument
, 0, OptOnlyHexDump
},
90 { "skip-sha", no_argument
, 0, OptSkipSHA
},
91 { "hide-serial-numbers", no_argument
, 0, OptHideSerialNumbers
},
92 { "replace-unique-ids", no_argument
, 0, OptReplaceUniqueIDs
},
93 { "version", no_argument
, 0, OptVersion
},
94 { "check-inline", no_argument
, 0, OptCheckInline
},
95 { "check", no_argument
, 0, OptCheck
},
96 { "short-timings", no_argument
, 0, OptShortTimings
},
97 { "long-timings", no_argument
, 0, OptLongTimings
},
98 { "ntsc", no_argument
, 0, OptNTSC
},
99 { "xmodeline", no_argument
, 0, OptXModeLineTimings
},
100 { "fbmode", no_argument
, 0, OptFBModeTimings
},
101 { "v4l2-timings", no_argument
, 0, OptV4L2Timings
},
102 { "diagonal", required_argument
, 0, OptDiag
},
103 { "std", required_argument
, 0, OptSTD
},
104 { "dmt", required_argument
, 0, OptDMT
},
105 { "vic", required_argument
, 0, OptVIC
},
106 { "hdmi-vic", required_argument
, 0, OptHDMIVIC
},
107 { "cvt", required_argument
, 0, OptCVT
},
108 { "gtf", required_argument
, 0, OptGTF
},
109 { "ovt", required_argument
, 0, OptOVT
},
110 { "list-established-timings", no_argument
, 0, OptListEstTimings
},
111 { "list-dmts", no_argument
, 0, OptListDMTs
},
112 { "list-vics", no_argument
, 0, OptListVICs
},
113 { "list-hdmi-vics", no_argument
, 0, OptListHDMIVICs
},
114 { "list-rid-timings", required_argument
, 0, OptListRIDTimings
},
115 { "list-rids", no_argument
, 0, OptListRIDs
},
119 static void usage(void)
121 printf("Usage: edid-decode <options> [in [out]]\n"
122 " [in] EDID file to parse. Read from standard input if none given\n"
123 " or if the input filename is '-'.\n"
124 " [out] Output the read EDID to this file. Write to standard output\n"
125 " if the output filename is '-'.\n"
127 " -o, --output-format <fmt>\n"
128 " If [out] is specified, then write the EDID in this format.\n"
129 " <fmt> is one of:\n"
130 " hex: hex numbers in ascii text (default for stdout)\n"
131 " raw: binary data (default unless writing to stdout)\n"
132 " carray: c-program struct\n"
134 " -c, --check Check if the EDID conforms to the standards, failures and\n"
135 " warnings are reported at the end.\n"
136 " -C, --check-inline Check if the EDID conforms to the standards, failures and\n"
137 " warnings are reported inline.\n"
138 " -n, --native-resolution Report the native resolution.\n"
139 " -p, --preferred-timings Report the preferred timings.\n"
140 " -P, --physical-address Only report the CEC physical address.\n"
141 " -S, --short-timings Report all video timings in a short format.\n"
142 " -L, --long-timings Report all video timings in a long format.\n"
143 " -N, --ntsc Report the video timings suitable for NTSC-based video.\n"
144 " -X, --xmodeline Report all long video timings in Xorg.conf format.\n"
145 " -F, --fbmode Report all long video timings in fb.modes format.\n"
146 " -V, --v4l2-timings Report all long video timings in v4l2-dv-timings.h format.\n"
147 " -s, --skip-hex-dump Skip the initial hex dump of the EDID.\n"
148 " -H, --only-hex-dump Only output the hex dump of the EDID.\n"
149 " --skip-sha Skip the SHA report.\n"
150 " --hide-serial-numbers Hide serial numbers with '...'.\n"
151 " --replace-unique-ids Replace unique IDs (serial numbers, Container IDs) with fixed values.\n"
152 " --version Show the edid-decode version (SHA).\n"
153 " --diagonal <inches> Set the display's diagonal in inches.\n"
154 " --std <byte1>,<byte2> Show the standard timing represented by these two bytes.\n"
155 " --dmt <dmt> Show the timings for the DMT with the given DMT ID.\n"
156 " --vic <vic> Show the timings for this VIC.\n"
157 " --hdmi-vic <hdmivic> Show the timings for this HDMI VIC.\n"
158 " --cvt w=<width>,h=<height>,fps=<fps>[,rb=<rb>][,interlaced][,overscan][,alt][,hblank=<hblank>][,vblank=<vblank>][,early-vsync]\n"
159 " Calculate the CVT timings for the given format.\n"
160 " <fps> is frames per second for progressive timings,\n"
161 " or fields per second for interlaced timings.\n"
162 " <rb> can be 0 (no reduced blanking, default), or\n"
163 " 1-3 for the reduced blanking version.\n"
164 " If 'interlaced' is given, then this is an interlaced format.\n"
165 " If 'overscan' is given, then this is an overscanned format.\n"
166 " If 'alt' is given and <rb>=2, then report the timings\n"
167 " optimized for video: 1000 / 1001 * <fps>.\n"
168 " If 'alt' is given and <rb>=3, then the horizontal blanking\n"
169 " is 160 instead of 80 pixels.\n"
170 " If 'hblank' is given and <rb>=3, then the horizontal blanking\n"
171 " is <hblank> pixels (range of 80-200), overriding 'alt'.\n"
172 " If 'vblank' is given and <rb>=3, then the vertical blanking\n"
173 " time is <vblank> microseconds (range of 460-705).\n"
174 " If 'early-vsync' is given and <rb=3>, then select early vsync.\n"
175 " --gtf w=<width>,h=<height>[,fps=<fps>][,horfreq=<horfreq>][,pixclk=<pixclk>][,interlaced]\n"
176 " [,overscan][,secondary][,C=<c>][,M=<m>][,K=<k>][,J=<j>]\n"
177 " Calculate the GTF timings for the given format.\n"
178 " <fps> is frames per second for progressive timings,\n"
179 " or fields per second for interlaced timings.\n"
180 " <horfreq> is the horizontal frequency in kHz.\n"
181 " <pixclk> is the pixel clock frequency in MHz.\n"
182 " Only one of fps, horfreq or pixclk must be given.\n"
183 " If 'interlaced' is given, then this is an interlaced format.\n"
184 " If 'overscan' is given, then this is an overscanned format.\n"
185 " If 'secondary' is given, then the secondary GTF is used for\n"
186 " reduced blanking, where <c>, <m>, <k> and <j> are parameters\n"
187 " for the secondary curve.\n"
188 " --ovt (rid=<rid>|w=<width>,h=<height>),fps=<fps>\n"
189 " Calculate the OVT timings for the given format.\n"
190 " Either specify a RID or explicitly specify width and height.\n"
191 " --list-established-timings List all known Established Timings.\n"
192 " --list-dmts List all known DMTs.\n"
193 " --list-vics List all known VICs.\n"
194 " --list-hdmi-vics List all known HDMI VICs.\n"
195 " --list-rids List all known RIDs.\n"
196 " --list-rid-timings <rid> List all timings for RID <rid> or all known RIDs if <rid> is 0.\n"
197 " -h, --help Display this help message.\n");
201 static std::string s_msgs
[EDID_MAX_BLOCKS
+ 1][2];
203 void msg(bool is_warn
, const char *fmt
, ...)
209 vsprintf(buf
, fmt
, ap
);
216 if (state
.data_block
.empty())
217 s_msgs
[state
.block_nr
][is_warn
] += std::string(" ") + buf
;
219 s_msgs
[state
.block_nr
][is_warn
] += " " + state
.data_block
+ ": " + buf
;
221 if (options
[OptCheckInline
])
222 printf("%s: %s", is_warn
? "WARN" : "FAIL", buf
);
225 static void show_msgs(bool is_warn
)
227 printf("\n%s:\n\n", is_warn
? "Warnings" : "Failures");
228 for (unsigned i
= 0; i
< state
.num_blocks
; i
++) {
229 if (s_msgs
[i
][is_warn
].empty())
231 printf("Block %u, %s:\n%s",
232 i
, block_name(edid
[i
* EDID_PAGE_SIZE
]).c_str(),
233 s_msgs
[i
][is_warn
].c_str());
235 if (s_msgs
[EDID_MAX_BLOCKS
][is_warn
].empty())
238 s_msgs
[EDID_MAX_BLOCKS
][is_warn
].c_str());
242 void replace_checksum(unsigned char *x
, size_t len
)
244 unsigned char sum
= 0;
247 for (i
= 0; i
< len
- 1; i
++)
249 x
[len
- 1] = -sum
& 0xff;
252 void do_checksum(const char *prefix
, const unsigned char *x
, size_t len
, unsigned unused_bytes
)
254 unsigned char check
= x
[len
- 1];
255 unsigned char sum
= 0;
258 for (i
= 0; i
< len
- 1; i
++)
261 printf("%sChecksum: 0x%02hhx", prefix
, check
);
262 if ((unsigned char)(check
+ sum
) != 0) {
263 printf(" (should be 0x%02x)", -sum
& 0xff);
264 fail("Invalid checksum 0x%02x (should be 0x%02x).\n",
268 printf(" Unused space in Extension Block: %u byte%s",
269 unused_bytes
, unused_bytes
> 1 ? "s" : "");
273 unsigned gcd(unsigned a
, unsigned b
)
284 void calc_ratio(struct timings
*t
)
286 unsigned d
= gcd(t
->hact
, t
->vact
);
289 t
->hratio
= t
->vratio
= 0;
292 t
->hratio
= t
->hact
/ d
;
293 t
->vratio
= t
->vact
/ d
;
296 std::string
edid_state::dtd_type(unsigned cnt
)
298 unsigned len
= std::to_string(cta
.preparsed_total_dtds
).length();
300 sprintf(buf
, "DTD %*u", len
, cnt
);
304 bool edid_state::match_timings(const timings
&t1
, const timings
&t2
)
306 if (t1
.hact
!= t2
.hact
||
307 t1
.vact
!= t2
.vact
||
309 t1
.interlaced
!= t2
.interlaced
||
312 t1
.hsync
!= t2
.hsync
||
313 t1
.pos_pol_hsync
!= t2
.pos_pol_hsync
||
314 t1
.hratio
!= t2
.hratio
||
317 t1
.vsync
!= t2
.vsync
||
318 t1
.pos_pol_vsync
!= t2
.pos_pol_vsync
||
319 t1
.vratio
!= t2
.vratio
||
320 t1
.pixclk_khz
!= t2
.pixclk_khz
)
325 static void or_str(std::string
&s
, const std::string
&flag
, unsigned &num_flags
)
329 else if (num_flags
% 2 == 0)
330 s
= s
+ " | \\\n\t\t" + flag
;
332 s
= s
+ " | " + flag
;
337 * Return true if the timings are a close, but not identical,
338 * match. The only differences allowed are polarities and
339 * porches and syncs, provided the total blanking remains the
342 bool timings_close_match(const timings
&t1
, const timings
&t2
)
344 // We don't want to deal with borders, you're on your own
345 // if you are using those.
346 if (t1
.hborder
|| t1
.vborder
||
347 t2
.hborder
|| t2
.vborder
)
349 if (t1
.hact
!= t2
.hact
|| t1
.vact
!= t2
.vact
||
350 t1
.interlaced
!= t2
.interlaced
||
351 t1
.pixclk_khz
!= t2
.pixclk_khz
||
352 t1
.hfp
+ t1
.hsync
+ t1
.hbp
!= t2
.hfp
+ t2
.hsync
+ t2
.hbp
||
353 t1
.vfp
+ t1
.vsync
+ t1
.vbp
!= t2
.vfp
+ t2
.vsync
+ t2
.vbp
)
355 if (t1
.hfp
== t2
.hfp
&&
356 t1
.hsync
== t2
.hsync
&&
358 t1
.pos_pol_hsync
== t2
.pos_pol_hsync
&&
360 t1
.vsync
== t2
.vsync
&&
362 t1
.pos_pol_vsync
== t2
.pos_pol_vsync
)
367 static void print_modeline(unsigned indent
, const struct timings
*t
, double refresh
)
369 unsigned offset
= (!t
->even_vtotal
&& t
->interlaced
) ? 1 : 0;
370 unsigned hfp
= t
->hborder
+ t
->hfp
;
371 unsigned hbp
= t
->hborder
+ t
->hbp
;
372 unsigned vfp
= t
->vborder
+ t
->vfp
;
373 unsigned vbp
= t
->vborder
+ t
->vbp
;
375 printf("%*sModeline \"%ux%u_%.2f%s\" %.3f %u %u %u %u %u %u %u %u %cHSync",
377 t
->hact
, t
->vact
, refresh
,
378 t
->interlaced
? "i" : "", t
->pixclk_khz
/ 1000.0,
379 t
->hact
, t
->hact
+ hfp
, t
->hact
+ hfp
+ t
->hsync
,
380 t
->hact
+ hfp
+ t
->hsync
+ hbp
,
381 t
->vact
, t
->vact
+ vfp
, t
->vact
+ vfp
+ t
->vsync
,
382 t
->vact
+ vfp
+ t
->vsync
+ vbp
+ offset
,
383 t
->pos_pol_hsync
? '+' : '-');
384 if (!t
->no_pol_vsync
)
385 printf(" %cVSync", t
->pos_pol_vsync
? '+' : '-');
387 printf(" Interlace");
391 static void print_fbmode(unsigned indent
, const struct timings
*t
,
392 double refresh
, double hor_freq_khz
)
394 printf("%*smode \"%ux%u-%u%s\"\n",
397 (unsigned)(0.5 + (t
->interlaced
? refresh
/ 2.0 : refresh
)),
398 t
->interlaced
? "-lace" : "");
399 printf("%*s# D: %.2f MHz, H: %.3f kHz, V: %.2f Hz\n",
401 t
->pixclk_khz
/ 1000.0, hor_freq_khz
, refresh
);
402 printf("%*sgeometry %u %u %u %u 32\n",
404 t
->hact
, t
->vact
, t
->hact
, t
->vact
);
405 unsigned mult
= t
->interlaced
? 2 : 1;
406 unsigned offset
= !t
->even_vtotal
&& t
->interlaced
;
407 unsigned hfp
= t
->hborder
+ t
->hfp
;
408 unsigned hbp
= t
->hborder
+ t
->hbp
;
409 unsigned vfp
= t
->vborder
+ t
->vfp
;
410 unsigned vbp
= t
->vborder
+ t
->vbp
;
411 printf("%*stimings %llu %d %d %d %u %u %u\n",
413 (unsigned long long)(1000000000.0 / (double)(t
->pixclk_khz
) + 0.5),
414 hbp
, hfp
, mult
* vbp
, mult
* vfp
+ offset
, t
->hsync
, mult
* t
->vsync
);
416 printf("%*slaced true\n", indent
+ 8, "");
417 if (t
->pos_pol_hsync
)
418 printf("%*shsync high\n", indent
+ 8, "");
419 if (t
->pos_pol_vsync
)
420 printf("%*svsync high\n", indent
+ 8, "");
421 printf("%*sendmode\n", indent
, "");
424 static void print_v4l2_timing(const struct timings
*t
,
425 double refresh
, const char *type
)
427 printf("\t#define V4L2_DV_BT_%uX%u%c%u_%02u { \\\n",
428 t
->hact
, t
->vact
, t
->interlaced
? 'I' : 'P',
429 (unsigned)refresh
, (unsigned)(0.5 + 100.0 * (refresh
- (unsigned)refresh
)));
430 printf("\t\t.type = V4L2_DV_BT_656_1120, \\\n");
431 printf("\t\tV4L2_INIT_BT_TIMINGS(%u, %u, %u, ",
432 t
->hact
, t
->vact
, t
->interlaced
);
433 if (!t
->pos_pol_hsync
&& !t
->pos_pol_vsync
)
435 else if (t
->pos_pol_hsync
&& t
->pos_pol_vsync
)
436 printf("\\\n\t\t\tV4L2_DV_HSYNC_POS_POL | V4L2_DV_VSYNC_POS_POL, \\\n");
437 else if (t
->pos_pol_hsync
)
438 printf("V4L2_DV_HSYNC_POS_POL, \\\n");
440 printf("V4L2_DV_VSYNC_POS_POL, \\\n");
441 unsigned hfp
= t
->hborder
+ t
->hfp
;
442 unsigned hbp
= t
->hborder
+ t
->hbp
;
443 unsigned vfp
= t
->vborder
+ t
->vfp
;
444 unsigned vbp
= t
->vborder
+ t
->vbp
;
445 printf("\t\t\t%lluULL, %d, %u, %d, %u, %u, %d, %u, %u, %d, \\\n",
446 t
->pixclk_khz
* 1000ULL, hfp
, t
->hsync
, hbp
,
448 t
->interlaced
? vfp
: 0,
449 t
->interlaced
? t
->vsync
: 0,
450 t
->interlaced
? vbp
+ !t
->even_vtotal
: 0);
453 unsigned num_flags
= 0;
455 unsigned hdmi_vic
= 0;
456 const char *std
= "0";
458 if (t
->interlaced
&& !t
->even_vtotal
)
459 or_str(flags
, "V4L2_DV_FL_HALF_LINE", num_flags
);
460 if (!memcmp(type
, "VIC", 3)) {
461 or_str(flags
, "V4L2_DV_FL_HAS_CEA861_VIC", num_flags
);
462 or_str(flags
, "V4L2_DV_FL_IS_CE_VIDEO", num_flags
);
463 vic
= strtoul(type
+ 4, 0, 0);
465 if (!memcmp(type
, "HDMI VIC", 8)) {
466 or_str(flags
, "V4L2_DV_FL_HAS_HDMI_VIC", num_flags
);
467 or_str(flags
, "V4L2_DV_FL_IS_CE_VIDEO", num_flags
);
468 hdmi_vic
= strtoul(type
+ 9, 0, 0);
469 vic
= hdmi_vic_to_vic(hdmi_vic
);
471 or_str(flags
, "V4L2_DV_FL_HAS_CEA861_VIC", num_flags
);
473 if (vic
&& (fmod(refresh
, 6)) == 0.0)
474 or_str(flags
, "V4L2_DV_FL_CAN_REDUCE_FPS", num_flags
);
476 or_str(flags
, "V4L2_DV_FL_REDUCED_BLANKING", num_flags
);
477 if (t
->hratio
&& t
->vratio
)
478 or_str(flags
, "V4L2_DV_FL_HAS_PICTURE_ASPECT", num_flags
);
480 if (!memcmp(type
, "VIC", 3) || !memcmp(type
, "HDMI VIC", 8))
481 std
= "V4L2_DV_BT_STD_CEA861";
482 else if (!memcmp(type
, "DMT", 3))
483 std
= "V4L2_DV_BT_STD_DMT";
484 else if (!memcmp(type
, "CVT", 3))
485 std
= "V4L2_DV_BT_STD_CVT";
486 else if (!memcmp(type
, "GTF", 3))
487 std
= "V4L2_DV_BT_STD_GTF";
488 printf("\t\t\t%s, \\\n", std
);
489 printf("\t\t\t%s, \\\n", flags
.empty() ? "0" : flags
.c_str());
490 printf("\t\t\t{ %u, %u }, %u, %u) \\\n",
491 t
->hratio
, t
->vratio
, vic
, hdmi_vic
);
495 static void print_detailed_timing(unsigned indent
, const struct timings
*t
)
497 printf("%*sHfront %4d Hsync %3u Hback %4d Hpol %s",
499 t
->hfp
, t
->hsync
, t
->hbp
, t
->pos_pol_hsync
? "P" : "N");
501 printf(" Hborder %u", t
->hborder
);
504 printf("%*sVfront %4u Vsync %3u Vback %4d",
505 indent
, "", t
->vfp
, t
->vsync
, t
->vbp
);
506 if (!t
->no_pol_vsync
)
507 printf(" Vpol %s", t
->pos_pol_vsync
? "P" : "N");
509 printf(" Vborder %u", t
->vborder
);
510 if (t
->even_vtotal
) {
511 printf(" Both Fields");
512 } else if (t
->interlaced
) {
513 printf(" Vfront +0.5 Odd Field\n");
514 printf("%*sVfront %4d Vsync %3u Vback %4d",
515 indent
, "", t
->vfp
, t
->vsync
, t
->vbp
);
516 if (!t
->no_pol_vsync
)
517 printf(" Vpol %s", t
->pos_pol_vsync
? "P" : "N");
519 printf(" Vborder %u", t
->vborder
);
520 printf(" Vback +0.5 Even Field");
525 bool edid_state::print_timings(const char *prefix
, const struct timings
*t
,
526 const char *type
, const char *flags
,
527 bool detailed
, bool do_checks
)
532 fail("Unknown video timings.\n");
536 if (detailed
&& options
[OptShortTimings
])
538 if (options
[OptLongTimings
])
541 unsigned vact
= t
->vact
;
542 unsigned hbl
= t
->hfp
+ t
->hsync
+ t
->hbp
+ 2 * t
->hborder
;
543 unsigned vbl
= t
->vfp
+ t
->vsync
+ t
->vbp
+ 2 * t
->vborder
;
544 unsigned htotal
= t
->hact
+ hbl
;
545 double hor_freq_khz
= htotal
? (double)t
->pixclk_khz
/ htotal
: 0;
550 double out_hor_freq_khz
= hor_freq_khz
;
554 double vtotal
= vact
+ vbl
;
558 if (!t
->hact
|| !hbl
|| !t
->hfp
|| !t
->hsync
||
559 !vact
|| !vbl
|| (!t
->vfp
&& !t
->interlaced
&& !t
->even_vtotal
) || !t
->vsync
) {
561 fail("0 values in the video timing:\n"
562 " Horizontal Active/Blanking %u/%u\n"
563 " Horizontal Frontporch/Sync Width %u/%u\n"
564 " Vertical Active/Blanking %u/%u\n"
565 " Vertical Frontporch/Sync Width %u/%u\n",
566 t
->hact
, hbl
, t
->hfp
, t
->hsync
, vact
, vbl
, t
->vfp
, t
->vsync
);
571 vtotal
= vact
+ t
->vfp
+ t
->vsync
+ t
->vbp
;
572 else if (t
->interlaced
)
573 vtotal
= vact
+ t
->vfp
+ t
->vsync
+ t
->vbp
+ 0.5;
575 double refresh
= t
->pixclk_khz
* 1000.0 / (htotal
* vtotal
);
576 double pixclk
= t
->pixclk_khz
* 1000.0;
577 if (options
[OptNTSC
] && fmod(refresh
, 6.0) == 0) {
578 const double ntsc_fact
= 1000.0 / 1001.0;
580 refresh
*= ntsc_fact
;
581 out_hor_freq_khz
*= ntsc_fact
;
585 unsigned rb
= t
->rb
& ~RB_ALT
;
587 bool alt
= t
->rb
& RB_ALT
;
590 s
+= std::string("v2") + (alt
? ",video-optimized" : "");
591 else if (rb
== RB_CVT_V3
)
592 s
+= std::string("v3") + (alt
? ",h-blank-160" : "");
595 if (t
->hsize_mm
|| t
->vsize_mm
)
596 add_str(s
, std::to_string(t
->hsize_mm
) + " mm x " + std::to_string(t
->vsize_mm
) + " mm");
597 if (t
->hsize_mm
> dtd_max_hsize_mm
)
598 dtd_max_hsize_mm
= t
->hsize_mm
;
599 if (t
->vsize_mm
> dtd_max_vsize_mm
)
600 dtd_max_vsize_mm
= t
->vsize_mm
;
603 unsigned pixclk_khz
= t
->pixclk_khz
/ (t
->ycbcr420
? 2 : 1);
607 sprintf(buf
, "%u%s", t
->vact
, t
->interlaced
? "i" : "");
608 printf("%s%s: %5ux%-5s %10.6f Hz %3u:%-3u %8.3f kHz %13.6f MHz%s\n",
612 t
->hratio
, t
->vratio
,
617 unsigned len
= strlen(prefix
) + 2;
619 if (!t
->ycbcr420
&& detailed
&& options
[OptXModeLineTimings
])
620 print_modeline(len
, t
, refresh
);
621 else if (!t
->ycbcr420
&& detailed
&& options
[OptFBModeTimings
])
622 print_fbmode(len
, t
, refresh
, hor_freq_khz
);
623 else if (!t
->ycbcr420
&& detailed
&& options
[OptV4L2Timings
])
624 print_v4l2_timing(t
, refresh
, type
);
626 print_detailed_timing(len
+ strlen(type
) + 6, t
);
631 if (!memcmp(type
, "DTD", 3)) {
633 const timings
*vic_t
= cta_close_match_to_vic(*t
, vic
);
636 warn("DTD is similar but not identical to VIC %u.\n", vic
);
638 const timings
*dmt_t
= close_match_to_dmt(*t
, dmt
);
640 warn("DTD is similar but not identical to DMT 0x%02x.\n", dmt
);
644 min_vert_freq_hz
= min(min_vert_freq_hz
, refresh
);
645 max_vert_freq_hz
= max(max_vert_freq_hz
, refresh
);
648 min_hor_freq_hz
= min(min_hor_freq_hz
, hor_freq_khz
* 1000.0);
649 max_hor_freq_hz
= max(max_hor_freq_hz
, hor_freq_khz
* 1000.0);
650 max_pixclk_khz
= max(max_pixclk_khz
, pixclk_khz
);
651 if (t
->pos_pol_hsync
&& !t
->pos_pol_vsync
&& t
->vsync
== 3)
652 base
.max_pos_neg_hor_freq_khz
= hor_freq_khz
;
655 if (t
->ycbcr420
&& t
->pixclk_khz
< 590000)
656 warn_once("Some YCbCr 4:2:0 timings are invalid for HDMI 2.1 (which requires an RGB timings pixel rate >= 590 MHz).\n");
658 fail("0 or negative horizontal front porch.\n");
660 fail("0 or negative horizontal back porch.\n");
662 fail("0 or negative vertical back porch.\n");
663 if (!base
.max_display_width_mm
&& !base
.max_display_height_mm
) {
665 } else if (!t
->hsize_mm
&& !t
->vsize_mm
) {
667 } else if (t
->hsize_mm
> base
.max_display_width_mm
+ 9 ||
668 t
->vsize_mm
> base
.max_display_height_mm
+ 9) {
669 fail("Mismatch of image size %ux%u mm vs display size %ux%u mm.\n",
670 t
->hsize_mm
, t
->vsize_mm
, base
.max_display_width_mm
, base
.max_display_height_mm
);
671 } else if (t
->hsize_mm
< base
.max_display_width_mm
- 9 &&
672 t
->vsize_mm
< base
.max_display_height_mm
- 9) {
673 fail("Mismatch of image size %ux%u mm vs display size %ux%u mm.\n",
674 t
->hsize_mm
, t
->vsize_mm
, base
.max_display_width_mm
, base
.max_display_height_mm
);
679 std::string
containerid2s(const unsigned char *x
)
683 sprintf(buf
, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
684 x
[0], x
[1], x
[2], x
[3],
688 x
[10], x
[11], x
[12], x
[13], x
[14], x
[15]);
692 std::string
utohex(unsigned char x
)
696 sprintf(buf
, "0x%02hhx", x
);
700 const char *oui_name(unsigned oui
, unsigned *ouinum
)
702 unsigned ouinumscratch
;
703 if (!ouinum
) ouinum
= &ouinumscratch
;
706 #define oneoui(c,k,n) case c: *ouinum = kOUI_##k; name = n; break;
708 default: *ouinum
= 0; name
= NULL
; break;
713 void edid_state::data_block_oui(std::string block_name
, const unsigned char *x
,
714 unsigned length
, unsigned *ouinum
, bool ignorezeros
, bool do_ascii
, bool big_endian
)
719 const char *ouiname
= NULL
;
720 bool matched_reverse
= false;
721 bool matched_ascii
= false;
722 bool valid_ascii
= false;
725 oui
= ((length
> 0 ? x
[0] : 0) << 16) + ((length
> 1 ? x
[1] : 0) << 8) + (length
> 2 ? x
[2] : 0);
727 oui
= ((length
> 2 ? x
[2] : 0) << 16) + ((length
> 1 ? x
[1] : 0) << 8) + (length
> 0 ? x
[0] : 0);
731 sprintf(ascii
, "?"); // some characters are null
732 if (ouinum
) *ouinum
= 0; // doesn't match a known OUI
734 valid_ascii
= (x
[0] >= 'A' && x
[1] >= 'A' && x
[2] >= 'A' && x
[0] <= 'Z' && x
[1] <= 'Z' && x
[2] <= 'Z');
735 sprintf(ascii
, "%c%c%c", x
[0], x
[1], x
[2]);
737 ouiname
= oui_name(oui
, ouinum
);
739 big_endian
= !big_endian
;
740 unsigned reversedoui
= ((oui
& 0xff) << 16) + (oui
& 0x00ff00) + (oui
>> 16);
741 ouiname
= oui_name(reversedoui
, ouinum
);
745 matched_reverse
= true;
746 } else if (do_ascii
&& valid_ascii
) {
747 unsigned asciioui
= (x
[0] << 24) + (x
[1] << 16) + (x
[2] << 8);
748 ouiname
= oui_name(asciioui
, ouinum
);
750 matched_ascii
= true;
759 name
= block_name
+ " (" + ouiname
+ ")" + ", PNP ID '" + ascii
+ "'";
761 name
= block_name
+ " (" + ouiname
+ ")" + ", OUI " + buf
;
762 } else if (do_ascii
&& valid_ascii
) {
763 name
= block_name
+ ", PNP ID '" + ascii
+ "'";
765 name
= block_name
+ ", OUI " + buf
;
767 // assign string to data_block before outputting errors
770 if (oui
|| !ignorezeros
) {
771 printf(" %s:\n", data_block
.c_str());
773 fail("Data block length (%d) is not enough to contain an OUI.\n", length
);
775 if (do_ascii
&& !valid_ascii
)
776 warn("Expected PNP ID but found OUI.\n");
778 fail("Endian-ness (%s) of OUI is different than expected (%s).\n", big_endian
? "be" : "le", big_endian
? "le" : "be");
782 warn("Unknown OUI %s (possible PNP %s).\n", buf
.c_str(), ascii
);
784 warn("Unknown OUI %s.\n", buf
.c_str());
789 std::string
ouitohex(unsigned oui
)
793 sprintf(buf
, "%02X-%02X-%02X", (oui
>> 16) & 0xff, (oui
>> 8) & 0xff, oui
& 0xff);
797 bool memchk(const unsigned char *x
, unsigned len
, unsigned char v
)
799 for (unsigned i
= 0; i
< len
; i
++)
805 void hex_block(const char *prefix
, const unsigned char *x
,
806 unsigned length
, bool show_ascii
, unsigned step
)
810 for (i
= 0; i
< length
; i
+= step
) {
811 unsigned len
= min(step
, length
- i
);
813 printf("%s", prefix
);
814 for (j
= 0; j
< len
; j
++)
815 printf("%s%02x", j
? " " : "", x
[i
+ j
]);
818 for (j
= len
; j
< step
; j
++)
821 for (j
= 0; j
< len
; j
++)
822 printf("%c", x
[i
+ j
] >= ' ' && x
[i
+ j
] <= '~' ? x
[i
+ j
] : '.');
829 static bool edid_add_byte(const char *s
, bool two_digits
= true)
833 if (state
.edid_size
== sizeof(edid
))
836 buf
[1] = two_digits
? s
[1] : 0;
838 edid
[state
.edid_size
++] = strtoul(buf
, NULL
, 16);
842 static bool extract_edid_quantumdata(const char *start
)
844 /* Parse QuantumData 980 EDID files */
846 start
= strstr(start
, ">");
850 for (unsigned i
= 0; start
[i
] && start
[i
+ 1] && i
< 256; i
+= 2)
851 if (!edid_add_byte(start
+ i
))
853 start
= strstr(start
, "<BLOCK");
855 return state
.edid_size
;
858 static const char *ignore_chars
= ",:;";
860 static bool extract_edid_hex(const char *s
, bool require_two_digits
= true)
863 if (isspace(*s
) || strchr(ignore_chars
, *s
))
866 if (*s
== '0' && tolower(s
[1]) == 'x') {
871 /* Read one or two hex digits from the log */
872 if (!isxdigit(s
[0])) {
873 if (state
.edid_size
&& state
.edid_size
% 128 == 0)
877 if (require_two_digits
&& !isxdigit(s
[1])) {
878 odd_hex_digits
= true;
881 if (!edid_add_byte(s
, isxdigit(s
[1])))
886 return state
.edid_size
;
889 static bool extract_edid_xrandr(const char *start
)
891 static const char indentation1
[] = " ";
892 static const char indentation2
[] = "\t\t";
893 /* Used to detect that we've gone past the EDID property */
894 static const char half_indentation1
[] = " ";
895 static const char half_indentation2
[] = "\t";
896 const char *indentation
;
902 /* Get the next start of the line of EDID hex, assuming spaces for indentation */
903 s
= strstr(start
, indentation
= indentation1
);
904 /* Did we skip the start of another property? */
905 if (s
&& s
> strstr(start
, half_indentation1
))
908 /* If we failed, retry assuming tabs for indentation */
910 s
= strstr(start
, indentation
= indentation2
);
911 /* Did we skip the start of another property? */
912 if (s
&& s
> strstr(start
, half_indentation2
))
919 start
= s
+ strlen(indentation
);
921 for (j
= 0; j
< 16; j
++, start
+= 2) {
922 /* Read a %02x from the log */
923 if (!isxdigit(start
[0]) || !isxdigit(start
[1])) {
928 if (!edid_add_byte(start
))
932 return state
.edid_size
;
935 static bool extract_edid_xorg(const char *start
)
937 bool find_first_num
= true;
939 for (; *start
; start
++) {
940 if (find_first_num
) {
943 /* skip ahead to the : */
944 s
= strstr(start
, ": \t");
946 s
= strstr(start
, ": ");
950 /* and find the first number */
951 while (!isxdigit(start
[1]))
953 find_first_num
= false;
956 /* Read a %02x from the log */
957 if (!isxdigit(*start
)) {
958 find_first_num
= true;
961 if (!edid_add_byte(start
))
966 return state
.edid_size
;
969 static bool extract_edid(int fd
, FILE *error
)
971 std::vector
<char> edid_data
;
972 char buf
[EDID_PAGE_SIZE
];
975 ssize_t i
= read(fd
, buf
, sizeof(buf
));
981 edid_data
.insert(edid_data
.end(), buf
, buf
+ i
);
984 if (edid_data
.empty()) {
989 const char *data
= &edid_data
[0];
992 /* Look for edid-decode output */
993 start
= strstr(data
, "EDID (hex):");
995 start
= strstr(data
, "edid-decode (hex):");
997 return extract_edid_hex(strchr(start
, ':'));
999 /* Look for C-array */
1000 start
= strstr(data
, "unsigned char edid[] = {");
1002 return extract_edid_hex(strchr(start
, '{') + 1, false);
1004 /* Look for QuantumData EDID output */
1005 start
= strstr(data
, "<BLOCK");
1007 return extract_edid_quantumdata(start
);
1009 /* Look for xrandr --verbose output (lines of 16 hex bytes) */
1010 start
= strstr(data
, "EDID_DATA:");
1012 start
= strstr(data
, "EDID:");
1014 return extract_edid_xrandr(start
);
1016 /* Look for an EDID in an Xorg.0.log file */
1017 start
= strstr(data
, "EDID (in hex):");
1019 start
= strstr(start
, "(II)");
1021 return extract_edid_xorg(start
);
1025 /* Is the EDID provided in hex? */
1026 for (i
= 0; i
< 32 && (isspace(data
[i
]) || strchr(ignore_chars
, data
[i
]) ||
1027 tolower(data
[i
]) == 'x' || isxdigit(data
[i
])); i
++);
1030 return extract_edid_hex(data
);
1033 if (edid_data
.size() > sizeof(edid
)) {
1034 fprintf(error
, "Binary EDID length %zu is greater than %zu.\n",
1035 edid_data
.size(), sizeof(edid
));
1038 memcpy(edid
, data
, edid_data
.size());
1039 state
.edid_size
= edid_data
.size();
1043 static int edid_from_file(const char *from_file
, FILE *error
)
1046 // Windows compatibility
1047 int flags
= O_RDONLY
| O_BINARY
;
1049 int flags
= O_RDONLY
;
1053 if (!strcmp(from_file
, "-")) {
1054 from_file
= "stdin";
1056 } else if ((fd
= open(from_file
, flags
)) == -1) {
1061 odd_hex_digits
= false;
1062 if (!extract_edid(fd
, error
)) {
1063 if (!state
.edid_size
) {
1064 fprintf(error
, "EDID of '%s' was empty.\n", from_file
);
1067 fprintf(error
, "EDID extract of '%s' failed: ", from_file
);
1069 fprintf(error
, "odd number of hexadecimal digits.\n");
1071 fprintf(error
, "unknown format.\n");
1074 if (state
.edid_size
% EDID_PAGE_SIZE
) {
1075 fprintf(error
, "EDID length %u is not a multiple of %u.\n",
1076 state
.edid_size
, EDID_PAGE_SIZE
);
1079 state
.num_blocks
= state
.edid_size
/ EDID_PAGE_SIZE
;
1083 if (memcmp(edid
, "\x00\xFF\xFF\xFF\xFF\xFF\xFF\x00", 8)) {
1084 fprintf(error
, "No EDID header found in '%s'.\n", from_file
);
1090 /* generic extension code */
1092 std::string
block_name(unsigned char block
)
1097 case 0x00: return "Base EDID";
1098 case 0x02: return "CTA-861 Extension Block";
1099 case 0x10: return "Video Timing Extension Block";
1100 case 0x20: return "EDID 2.0 Extension Block";
1101 case 0x40: return "Display Information Extension Block";
1102 case 0x50: return "Localized String Extension Block";
1103 case 0x60: return "Microdisplay Interface Extension Block";
1104 case 0x70: return "DisplayID Extension Block";
1105 case 0xf0: return "Block Map Extension Block";
1106 case 0xff: return "Manufacturer-Specific Extension Block";
1108 sprintf(buf
, " 0x%02x", block
);
1109 return std::string("Unknown EDID Extension Block") + buf
;
1113 void edid_state::parse_block_map(const unsigned char *x
)
1115 unsigned last_valid_block_tag
= 0;
1116 bool fail_once
= false;
1117 unsigned offset
= 1;
1121 block_map
.saw_block_1
= true;
1122 else if (!block_map
.saw_block_1
)
1123 fail("No EDID Block Map Extension found in block 1.\n");
1124 else if (block_nr
== 128)
1125 block_map
.saw_block_128
= true;
1130 for (i
= 1; i
< 127; i
++) {
1131 unsigned block
= offset
+ i
;
1134 last_valid_block_tag
++;
1135 if (i
!= last_valid_block_tag
&& !fail_once
) {
1136 fail("Valid block tags are not consecutive.\n");
1139 printf(" Block %3u: %s\n", block
, block_name(x
[i
]).c_str());
1140 if (block
>= num_blocks
) {
1142 fail("Invalid block number %u.\n", block
);
1144 } else if (x
[i
] != edid
[block
* EDID_PAGE_SIZE
]) {
1145 fail("Block %u tag mismatch: expected 0x%02x, but got 0x%02x.\n",
1146 block
, edid
[block
* EDID_PAGE_SIZE
], x
[i
]);
1148 } else if (block
< num_blocks
) {
1149 fail("Block %u tag mismatch: expected 0x%02x, but got 0x00.\n",
1150 block
, edid
[block
* EDID_PAGE_SIZE
]);
1155 void edid_state::preparse_extension(unsigned char *x
)
1160 preparse_cta_block(x
);
1163 preparse_ls_ext_block(x
);
1167 preparse_displayid_block(x
);
1172 void edid_state::parse_extension(const unsigned char *x
)
1174 block
= block_name(x
[0]);
1179 if (block_nr
&& x
[0] == 0)
1180 block
= "Unknown EDID Extension Block 0x00";
1181 printf("Block %u, %s:\n", block_nr
, block
.c_str());
1188 parse_vtb_ext_block(x
);
1191 fail("Deprecated extension block for EDID 2.0, do not use.\n");
1194 parse_di_ext_block(x
);
1197 parse_ls_ext_block(x
);
1200 parse_displayid_block(x
);
1204 if (block_nr
!= 1 && block_nr
!= 128)
1205 fail("Must be used in block 1 and 128.\n");
1208 hex_block(" ", x
, EDID_PAGE_SIZE
);
1209 fail("Unknown Extension Block.\n");
1214 do_checksum("", x
, EDID_PAGE_SIZE
, unused_bytes
);
1217 void edid_state::print_preferred_timings()
1219 if (base
.preferred_timing
.is_valid()) {
1220 printf("\n----------------\n");
1221 printf("\nPreferred Video Timing if only Block 0 is parsed:\n");
1222 print_timings(" ", base
.preferred_timing
, true, false);
1225 if (!cta
.preferred_timings
.empty()) {
1226 printf("\n----------------\n");
1227 printf("\nPreferred Video Timing%s if Block 0 and CTA-861 Blocks are parsed:\n",
1228 cta
.preferred_timings
.size() > 1 ? "s" : "");
1229 for (vec_timings_ext::iterator iter
= cta
.preferred_timings
.begin();
1230 iter
!= cta
.preferred_timings
.end(); ++iter
)
1231 print_timings(" ", *iter
, true, false);
1234 if (!cta
.preferred_timings_vfpdb
.empty()) {
1235 printf("\n----------------\n");
1236 printf("\nPreferred Video Timing%s if Block 0 and CTA-861 Blocks are parsed with VFPDB support:\n",
1237 cta
.preferred_timings_vfpdb
.size() > 1 ? "s" : "");
1238 for (vec_timings_ext::iterator iter
= cta
.preferred_timings_vfpdb
.begin();
1239 iter
!= cta
.preferred_timings_vfpdb
.end(); ++iter
)
1240 print_timings(" ", *iter
, true, false);
1243 if (!dispid
.preferred_timings
.empty()) {
1244 printf("\n----------------\n");
1245 printf("\nPreferred Video Timing%s if Block 0 and DisplayID Blocks are parsed:\n",
1246 dispid
.preferred_timings
.size() > 1 ? "s" : "");
1247 for (vec_timings_ext::iterator iter
= dispid
.preferred_timings
.begin();
1248 iter
!= dispid
.preferred_timings
.end(); ++iter
)
1249 print_timings(" ", *iter
, true, false);
1253 void edid_state::print_native_res()
1255 typedef std::pair
<unsigned, unsigned> resolution
;
1256 typedef std::set
<resolution
> resolution_set
;
1257 resolution_set native_prog
, native_int
, native_nvrdb
;
1258 unsigned native_width
= 0, native_height
= 0;
1259 unsigned native_width_int
= 0, native_height_int
= 0;
1261 // Note: it is also a mismatch if Block 0 does not define a
1262 // native resolution, but other blocks do.
1263 bool native_mismatch
= false;
1264 bool native_int_mismatch
= false;
1266 if (base
.preferred_timing
.is_valid() && base
.preferred_is_also_native
) {
1267 if (base
.preferred_timing
.t
.interlaced
) {
1268 native_width_int
= base
.preferred_timing
.t
.hact
;
1269 native_height_int
= base
.preferred_timing
.t
.vact
;
1271 native_width
= base
.preferred_timing
.t
.hact
;
1272 native_height
= base
.preferred_timing
.t
.vact
;
1276 if (!native_width
&& dispid
.native_width
) {
1277 native_width
= dispid
.native_width
;
1278 native_height
= dispid
.native_height
;
1279 native_mismatch
= true;
1280 } else if (dispid
.native_width
&& native_width
&&
1281 (dispid
.native_width
!= native_width
||
1282 dispid
.native_height
!= native_height
)) {
1283 native_mismatch
= true;
1286 for (vec_timings_ext::iterator iter
= cta
.native_timings
.begin();
1287 iter
!= cta
.native_timings
.end(); ++iter
) {
1288 if (iter
->t
.interlaced
) {
1289 native_int
.insert(std::pair
<unsigned, unsigned>(iter
->t
.hact
, iter
->t
.vact
));
1290 if (!native_width_int
) {
1291 native_width_int
= iter
->t
.hact
;
1292 native_height_int
= iter
->t
.vact
;
1293 native_int_mismatch
= true;
1294 } else if (native_width_int
&&
1295 (iter
->t
.hact
!= native_width_int
||
1296 iter
->t
.vact
!= native_height_int
)) {
1297 native_int_mismatch
= true;
1300 native_prog
.insert(std::pair
<unsigned, unsigned>(iter
->t
.hact
, iter
->t
.vact
));
1301 if (!native_width
) {
1302 native_width
= iter
->t
.hact
;
1303 native_height
= iter
->t
.vact
;
1304 native_mismatch
= true;
1305 } else if (native_width
&&
1306 (iter
->t
.hact
!= native_width
||
1307 iter
->t
.vact
!= native_height
)) {
1308 native_mismatch
= true;
1313 for (vec_timings_ext::iterator iter
= cta
.native_timing_nvrdb
.begin();
1314 iter
!= cta
.native_timing_nvrdb
.end(); ++iter
) {
1315 if (iter
->t
.interlaced
) {
1316 fail("Interlaced native timing in NVRDB.\n");
1318 native_nvrdb
.insert(std::pair
<unsigned, unsigned>(iter
->t
.hact
, iter
->t
.vact
));
1319 if (!native_width
) {
1320 native_width
= iter
->t
.hact
;
1321 native_height
= iter
->t
.vact
;
1322 native_mismatch
= true;
1323 } else if (native_width
&&
1324 (iter
->t
.hact
!= native_width
||
1325 iter
->t
.vact
!= native_height
)) {
1326 native_mismatch
= true;
1333 double w
= image_width
;
1334 double h
= image_height
;
1335 double d
= sqrt(w
* w
+ h
* h
) / 254.0;
1337 if (fabs(diagonal
- d
) >= 0.1)
1338 warn("Specified diagonal is %.1f\", calculated diagonal is %.1f\".\n",
1342 double w
= native_width
;
1343 double h
= native_height
;
1344 double d
= diagonal
* 254.0;
1345 double c
= sqrt((d
* d
) / (w
* w
+ h
* h
));
1351 if (fabs((double)image_width
- w
) >= 100.0 ||
1352 fabs((double)image_height
- h
) >= 100.0)
1353 warn("Calculated image size is %.1fx%.1fmm, EDID image size is %.1fx%.1fmm.\n",
1355 image_width
/ 10.0, image_height
/ 10.0);
1357 warn("No image size was specified, but it is calculated as %.1fx%.1fmm.\n",
1358 w
/ 10.0, h
/ 10.0);
1363 if (!options
[OptNativeResolution
])
1366 if (native_width
== 0 && native_width_int
== 0) {
1367 printf("\n----------------\n");
1368 printf("\nNo Native Video Resolution was defined.\n");
1372 if ((native_width
|| native_width_int
) &&
1373 !native_mismatch
&& !native_int_mismatch
) {
1374 printf("\n----------------\n");
1375 printf("\nNative Video Resolution%s:\n",
1376 native_width
&& native_width_int
? "s" : "");
1378 printf(" %ux%u\n", native_width
, native_height
);
1379 if (native_width_int
)
1380 printf(" %ux%ui\n", native_width_int
, native_height_int
);
1384 if (base
.preferred_timing
.is_valid() && base
.preferred_is_also_native
) {
1385 printf("\n----------------\n");
1386 printf("\nNative Video Resolution if only Block 0 is parsed:\n");
1387 printf(" %ux%u%s\n",
1388 base
.preferred_timing
.t
.hact
, base
.preferred_timing
.t
.vact
,
1389 base
.preferred_timing
.t
.interlaced
? "i" : "");
1392 if (!cta
.native_timings
.empty()) {
1393 printf("\n----------------\n");
1394 printf("\nNative Video Resolution%s if Block 0 and CTA-861 Blocks are parsed:\n",
1395 native_prog
.size() + native_int
.size() > 1 ? "s" : "");
1396 for (resolution_set::iterator iter
= native_prog
.begin();
1397 iter
!= native_prog
.end(); ++iter
)
1398 printf(" %ux%u\n", iter
->first
, iter
->second
);
1399 for (resolution_set::iterator iter
= native_int
.begin();
1400 iter
!= native_int
.end(); ++iter
)
1401 printf(" %ux%ui\n", iter
->first
, iter
->second
);
1404 if (!cta
.native_timing_nvrdb
.empty()) {
1405 printf("\n----------------\n");
1406 printf("\nNative Video Resolution if Block 0 and CTA-861 Blocks are parsed with NVRDB support:\n");
1407 for (resolution_set::iterator iter
= native_nvrdb
.begin();
1408 iter
!= native_nvrdb
.end(); ++iter
)
1409 printf(" %ux%u\n", iter
->first
, iter
->second
);
1412 if (dispid
.native_width
) {
1413 printf("\n----------------\n");
1414 printf("\nNative Video Resolution if the DisplayID Blocks are parsed:\n");
1415 printf(" %ux%u\n", dispid
.native_width
, dispid
.native_height
);
1419 int edid_state::parse_edid()
1421 hide_serial_numbers
= options
[OptHideSerialNumbers
];
1422 replace_unique_ids
= options
[OptReplaceUniqueIDs
];
1424 preparse_base_block(edid
);
1425 if (replace_unique_ids
)
1426 replace_checksum(edid
, EDID_PAGE_SIZE
);
1428 for (unsigned i
= 1; i
< num_blocks
; i
++)
1429 preparse_extension(edid
+ i
* EDID_PAGE_SIZE
);
1431 if (options
[OptPhysicalAddress
]) {
1432 printf("%x.%x.%x.%x\n",
1433 (cta
.preparsed_phys_addr
>> 12) & 0xf,
1434 (cta
.preparsed_phys_addr
>> 8) & 0xf,
1435 (cta
.preparsed_phys_addr
>> 4) & 0xf,
1436 cta
.preparsed_phys_addr
& 0xf);
1440 if (!options
[OptSkipHexDump
]) {
1441 printf("edid-decode (hex):\n\n");
1442 for (unsigned i
= 0; i
< num_blocks
; i
++) {
1443 hex_block("", edid
+ i
* EDID_PAGE_SIZE
, EDID_PAGE_SIZE
, false);
1444 if (i
== num_blocks
- 1 && options
[OptOnlyHexDump
])
1448 printf("----------------\n\n");
1451 block
= block_name(0x00);
1452 printf("Block %u, %s:\n", block_nr
, block
.c_str());
1453 parse_base_block(edid
);
1455 for (unsigned i
= 1; i
< num_blocks
; i
++) {
1457 printf("\n----------------\n");
1458 parse_extension(edid
+ i
* EDID_PAGE_SIZE
);
1462 block_nr
= EDID_MAX_BLOCKS
;
1467 if (options
[OptPreferredTimings
])
1468 print_preferred_timings();
1472 if (!options
[OptCheck
] && !options
[OptCheckInline
])
1475 check_base_block(edid
);
1479 check_displayid_blocks();
1481 printf("\n----------------\n");
1483 if (!options
[OptSkipSHA
] && strlen(STRING(SHA
))) {
1484 printf("\nedid-decode SHA: %s %s\n", STRING(SHA
), STRING(DATE
));
1487 if (options
[OptCheck
]) {
1493 printf("\nEDID conformity: %s\n", failures
? "FAIL" : "PASS");
1494 return failures
? -2 : 0;
1497 #ifndef __EMSCRIPTEN__
1499 static unsigned char crc_calc(const unsigned char *b
)
1501 unsigned char sum
= 0;
1504 for (i
= 0; i
< 127; i
++)
1509 static int crc_ok(const unsigned char *b
)
1511 return crc_calc(b
) == b
[127];
1514 static void hexdumpedid(FILE *f
, const unsigned char *edid
, unsigned size
)
1518 for (b
= 0; b
< size
/ 128; b
++) {
1519 const unsigned char *buf
= edid
+ 128 * b
;
1523 for (i
= 0; i
< 128; i
+= 0x10) {
1524 fprintf(f
, "%02x", buf
[i
]);
1525 for (j
= 1; j
< 0x10; j
++) {
1526 fprintf(f
, " %02x", buf
[i
+ j
]);
1531 fprintf(f
, "Block %u has a checksum error (should be 0x%02x).\n",
1536 static void carraydumpedid(FILE *f
, const unsigned char *edid
, unsigned size
)
1540 fprintf(f
, "const unsigned char edid[] = {\n");
1541 for (b
= 0; b
< size
/ 128; b
++) {
1542 const unsigned char *buf
= edid
+ 128 * b
;
1546 for (i
= 0; i
< 128; i
+= 8) {
1547 fprintf(f
, "\t0x%02x,", buf
[i
]);
1548 for (j
= 1; j
< 8; j
++) {
1549 fprintf(f
, " 0x%02x,", buf
[i
+ j
]);
1554 fprintf(f
, "\t/* Block %u has a checksum error (should be 0x%02x). */\n",
1560 // This format can be read by the QuantumData EDID editor
1561 static void xmldumpedid(FILE *f
, const unsigned char *edid
, unsigned size
)
1563 fprintf(f
, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
1564 fprintf(f
, "<DATAOBJ>\n");
1565 fprintf(f
, " <HEADER TYPE=\"DID\" VERSION=\"1.0\"/>\n");
1566 fprintf(f
, " <DATA>\n");
1567 for (unsigned b
= 0; b
< size
/ 128; b
++) {
1568 const unsigned char *buf
= edid
+ 128 * b
;
1570 fprintf(f
, " <BLOCK%u>", b
);
1571 for (unsigned i
= 0; i
< 128; i
++)
1572 fprintf(f
, "%02X", buf
[i
]);
1573 fprintf(f
, "</BLOCK%u>\n", b
);
1575 fprintf(f
, " </DATA>\n");
1576 fprintf(f
, "</DATAOBJ>\n");
1579 static int edid_to_file(const char *to_file
, enum output_format out_fmt
)
1583 if (!strcmp(to_file
, "-")) {
1586 } else if ((out
= fopen(to_file
, "w")) == NULL
) {
1590 if (out_fmt
== OUT_FMT_DEFAULT
)
1591 out_fmt
= out
== stdout
? OUT_FMT_HEX
: OUT_FMT_RAW
;
1596 hexdumpedid(out
, edid
, state
.edid_size
);
1599 fwrite(edid
, state
.edid_size
, 1, out
);
1601 case OUT_FMT_CARRAY
:
1602 carraydumpedid(out
, edid
, state
.edid_size
);
1605 xmldumpedid(out
, edid
, state
.edid_size
);
1627 static int parse_cvt_subopt(char **subopt_str
, double *value
)
1632 static const char * const subopt_list
[] = {
1646 opt
= getsubopt(subopt_str
, (char* const*) subopt_list
, &opt_str
);
1649 fprintf(stderr
, "Invalid suboptions specified.\n");
1651 std::exit(EXIT_FAILURE
);
1653 if (opt_str
== nullptr && opt
!= CVT_INTERLACED
&& opt
!= CVT_ALT
&&
1654 opt
!= CVT_OVERSCAN
&& opt
!= CVT_EARLY_VSYNC
) {
1655 fprintf(stderr
, "No value given to suboption <%s>.\n",
1658 std::exit(EXIT_FAILURE
);
1662 *value
= strtod(opt_str
, nullptr);
1666 static void parse_cvt(char *optarg
)
1668 unsigned w
= 0, h
= 0;
1670 unsigned rb
= RB_NONE
;
1671 unsigned rb_h_blank
= 0;
1672 unsigned rb_v_blank
= 460;
1673 bool interlaced
= false;
1675 bool overscan
= false;
1676 bool early_vsync
= false;
1678 while (*optarg
!= '\0') {
1682 opt
= parse_cvt_subopt(&optarg
, &opt_val
);
1700 case CVT_INTERLACED
:
1701 interlaced
= opt_val
;
1706 case CVT_RB_H_BLANK
:
1707 rb_h_blank
= opt_val
;
1709 case CVT_RB_V_BLANK
:
1710 rb_v_blank
= opt_val
;
1711 if (rb_v_blank
< 460) {
1712 fprintf(stderr
, "vblank must be >= 460, set to 460.\n");
1714 } else if (rb_v_blank
> 705) {
1715 fprintf(stderr
, "warning: vblank values > 705 might not be supported by RBv3 compliant sources.\n");
1718 case CVT_EARLY_VSYNC
:
1726 if (!w
|| !h
|| !fps
) {
1727 fprintf(stderr
, "Missing width, height and/or fps.\n");
1729 std::exit(EXIT_FAILURE
);
1733 timings t
= state
.calc_cvt_mode(w
, h
, fps
, rb
, interlaced
, overscan
, alt
,
1734 rb_h_blank
, rb_v_blank
, early_vsync
);
1735 state
.print_timings("", &t
, "CVT", "", true, false);
1738 struct gtf_parsed_data
{
1745 bool params_from_edid
;
1746 enum gtf_ip_parm ip_parm
;
1764 static int parse_gtf_subopt(char **subopt_str
, double *value
)
1769 static const char * const subopt_list
[] = {
1785 opt
= getsubopt(subopt_str
, (char * const *)subopt_list
, &opt_str
);
1788 fprintf(stderr
, "Invalid suboptions specified.\n");
1790 std::exit(EXIT_FAILURE
);
1792 if (opt_str
== nullptr && opt
!= GTF_INTERLACED
&& opt
!= GTF_OVERSCAN
&&
1793 opt
!= GTF_SECONDARY
) {
1794 fprintf(stderr
, "No value given to suboption <%s>.\n",
1797 std::exit(EXIT_FAILURE
);
1800 if (opt
== GTF_C2
|| opt
== GTF_J2
)
1801 *value
= round(2.0 * strtod(opt_str
, nullptr));
1803 *value
= strtod(opt_str
, nullptr);
1807 static void parse_gtf(char *optarg
, gtf_parsed_data
&data
)
1809 memset(&data
, 0, sizeof(data
));
1810 data
.params_from_edid
= true;
1816 while (*optarg
!= '\0') {
1820 opt
= parse_gtf_subopt(&optarg
, &opt_val
);
1824 data
.w
= round(opt_val
);
1827 data
.h
= round(opt_val
);
1830 data
.freq
= opt_val
;
1831 data
.ip_parm
= gtf_ip_vert_freq
;
1834 data
.freq
= opt_val
;
1835 data
.ip_parm
= gtf_ip_hor_freq
;
1838 data
.freq
= opt_val
;
1839 data
.ip_parm
= gtf_ip_clk_freq
;
1841 case GTF_INTERLACED
:
1842 data
.interlaced
= true;
1845 data
.overscan
= true;
1848 data
.secondary
= true;
1851 data
.C
= opt_val
/ 2.0;
1852 data
.params_from_edid
= false;
1855 data
.M
= round(opt_val
);
1856 data
.params_from_edid
= false;
1859 data
.K
= round(opt_val
);
1860 data
.params_from_edid
= false;
1863 data
.J
= opt_val
/ 2.0;
1864 data
.params_from_edid
= false;
1871 if (!data
.w
|| !data
.h
) {
1872 fprintf(stderr
, "Missing width and/or height.\n");
1874 std::exit(EXIT_FAILURE
);
1877 fprintf(stderr
, "One of fps, horfreq or pixclk must be given.\n");
1879 std::exit(EXIT_FAILURE
);
1881 if (!data
.secondary
)
1882 data
.params_from_edid
= false;
1883 if (data
.interlaced
&& data
.ip_parm
== gtf_ip_vert_freq
)
1887 static void show_gtf(gtf_parsed_data
&data
)
1891 t
= state
.calc_gtf_mode(data
.w
, data
.h
, data
.freq
, data
.interlaced
,
1892 data
.ip_parm
, data
.overscan
, data
.secondary
,
1893 data
.C
, data
.M
, data
.K
, data
.J
);
1895 state
.print_timings("", &t
, "GTF", "", true, false);
1905 static int parse_ovt_subopt(char **subopt_str
, unsigned *value
)
1910 static const char * const subopt_list
[] = {
1918 opt
= getsubopt(subopt_str
, (char* const*) subopt_list
, &opt_str
);
1921 fprintf(stderr
, "Invalid suboptions specified.\n");
1923 std::exit(EXIT_FAILURE
);
1925 if (opt_str
== nullptr) {
1926 fprintf(stderr
, "No value given to suboption <%s>.\n",
1929 std::exit(EXIT_FAILURE
);
1933 *value
= strtoul(opt_str
, NULL
, 0);
1937 static void parse_ovt(char *optarg
)
1940 unsigned w
= 0, h
= 0;
1943 while (*optarg
!= '\0') {
1947 opt
= parse_ovt_subopt(&optarg
, &opt_val
);
1967 if ((!rid
&& (!w
|| !h
)) || !fps
) {
1968 fprintf(stderr
, "Missing rid, width, height and/or fps.\n");
1970 std::exit(EXIT_FAILURE
);
1972 unsigned hratio
= 0, vratio
= 0;
1974 const cta_rid
*r
= find_rid(rid
);
1983 timings t
= state
.calc_ovt_mode(w
, h
, hratio
, vratio
, fps
);
1984 state
.print_timings("", &t
, "OVT", "", true, false);
1987 int main(int argc
, char **argv
)
1989 char short_options
[26 * 2 * 2 + 1];
1990 enum output_format out_fmt
= OUT_FMT_DEFAULT
;
1991 gtf_parsed_data gtf_data
;
1992 unsigned list_rid
= 0;
1996 int option_index
= 0;
2002 for (i
= 0; long_options
[i
].name
; i
++) {
2003 if (!isalpha(long_options
[i
].val
))
2005 short_options
[idx
++] = long_options
[i
].val
;
2006 if (long_options
[i
].has_arg
== required_argument
)
2007 short_options
[idx
++] = ':';
2009 short_options
[idx
] = 0;
2010 int ch
= getopt_long(argc
, argv
, short_options
,
2011 long_options
, &option_index
);
2020 case OptOutputFormat
:
2021 if (!strcmp(optarg
, "hex")) {
2022 out_fmt
= OUT_FMT_HEX
;
2023 } else if (!strcmp(optarg
, "raw")) {
2024 out_fmt
= OUT_FMT_RAW
;
2025 } else if (!strcmp(optarg
, "carray")) {
2026 out_fmt
= OUT_FMT_CARRAY
;
2027 } else if (!strcmp(optarg
, "xml")) {
2028 out_fmt
= OUT_FMT_XML
;
2035 state
.diagonal
= strtod(optarg
, NULL
);
2038 unsigned char byte1
, byte2
= 0;
2041 byte1
= strtoul(optarg
, &endptr
, 0);
2043 byte2
= strtoul(endptr
+ 1, NULL
, 0);
2044 state
.print_standard_timing("", byte1
, byte2
, false, true);
2048 val
= strtoul(optarg
, NULL
, 0);
2049 t
= find_dmt_id(val
);
2051 sprintf(buf
, "DMT 0x%02x", val
);
2052 state
.print_timings("", t
, buf
, "", true, false);
2054 fprintf(stderr
, "Unknown DMT code 0x%02x.\n", val
);
2058 val
= strtoul(optarg
, NULL
, 0);
2059 t
= find_vic_id(val
);
2061 sprintf(buf
, "VIC %3u", val
);
2062 state
.print_timings("", t
, buf
, "", true, false);
2064 fprintf(stderr
, "Unknown VIC code %u.\n", val
);
2068 val
= strtoul(optarg
, NULL
, 0);
2069 t
= find_hdmi_vic_id(val
);
2071 sprintf(buf
, "HDMI VIC %u", val
);
2072 state
.print_timings("", t
, buf
, "", true, false);
2074 fprintf(stderr
, "Unknown HDMI VIC code %u.\n", val
);
2081 parse_gtf(optarg
, gtf_data
);
2086 case OptListRIDTimings
:
2087 list_rid
= strtoul(optarg
, NULL
, 0);
2090 fprintf(stderr
, "Option '%s' requires a value.\n",
2095 fprintf(stderr
, "Unknown argument '%s'.\n",
2101 if (optind
== argc
&& options
[OptVersion
]) {
2102 if (strlen(STRING(SHA
)))
2103 printf("edid-decode SHA: %s %s\n", STRING(SHA
), STRING(DATE
));
2105 printf("edid-decode SHA: not available\n");
2109 if (options
[OptListEstTimings
])
2110 state
.list_established_timings();
2111 if (options
[OptListDMTs
])
2113 if (options
[OptListVICs
])
2114 state
.cta_list_vics();
2115 if (options
[OptListHDMIVICs
])
2116 state
.cta_list_hdmi_vics();
2117 if (options
[OptListRIDs
])
2118 state
.cta_list_rids();
2119 if (options
[OptListRIDTimings
])
2120 state
.cta_list_rid_timings(list_rid
);
2122 if (options
[OptListEstTimings
] || options
[OptListDMTs
] ||
2123 options
[OptListVICs
] || options
[OptListHDMIVICs
] ||
2124 options
[OptListRIDs
] || options
[OptListRIDTimings
])
2127 if (options
[OptCVT
] || options
[OptDMT
] || options
[OptVIC
] ||
2128 options
[OptHDMIVIC
] || options
[OptSTD
] || options
[OptOVT
])
2131 if (options
[OptGTF
] && (!gtf_data
.params_from_edid
|| optind
== argc
)) {
2137 ret
= edid_from_file("-", stdout
);
2139 ret
= edid_from_file(argv
[optind
], argv
[optind
+ 1] ? stderr
: stdout
);
2141 if (ret
&& options
[OptPhysicalAddress
]) {
2142 printf("f.f.f.f\n");
2145 if (optind
< argc
- 1)
2146 return ret
? ret
: edid_to_file(argv
[optind
+ 1], out_fmt
);
2148 if (options
[OptGTF
]) {
2151 state
.preparse_base_block(edid
);
2153 t
= state
.calc_gtf_mode(gtf_data
.w
, gtf_data
.h
, gtf_data
.freq
,
2154 gtf_data
.interlaced
, gtf_data
.ip_parm
,
2156 unsigned hbl
= t
.hfp
+ t
.hsync
+ t
.hbp
;
2157 unsigned htotal
= t
.hact
+ hbl
;
2158 double hor_freq_khz
= htotal
? (double)t
.pixclk_khz
/ htotal
: 0;
2160 if (state
.base
.supports_sec_gtf
&&
2161 hor_freq_khz
>= state
.base
.sec_gtf_start_freq
) {
2162 t
= state
.calc_gtf_mode(gtf_data
.w
, gtf_data
.h
, gtf_data
.freq
,
2163 gtf_data
.interlaced
, gtf_data
.ip_parm
,
2164 gtf_data
.overscan
, true,
2165 state
.base
.C
, state
.base
.M
,
2166 state
.base
.K
, state
.base
.J
);
2170 state
.print_timings("", &t
, "GTF", "INVALID: Hfront <= 0", true, false);
2172 state
.print_timings("", &t
, "GTF", "", true, false);
2176 return ret
? ret
: state
.parse_edid();
2182 * The surrounding JavaScript implementation will call this function
2183 * each time it wants to decode an EDID. So this should reset all the
2184 * state and start over.
2186 extern "C" int parse_edid(const char *input
)
2188 for (unsigned i
= 0; i
< EDID_MAX_BLOCKS
+ 1; i
++) {
2189 s_msgs
[i
][0].clear();
2190 s_msgs
[i
][1].clear();
2192 options
[OptCheck
] = 1;
2193 options
[OptPreferredTimings
] = 1;
2194 options
[OptNativeResolution
] = 1;
2195 state
= edid_state();
2196 int ret
= edid_from_file(input
, stderr
);
2197 return ret
? ret
: state
.parse_edid();