edid-decode: build edid-decode.js without warnings
[edid-decode.git] / edid-decode.cpp
blobb7fe57ad84746541d0c157385115debb2fa7a289
1 // SPDX-License-Identifier: MIT
2 /*
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>
8 */
10 #include <ctype.h>
11 #include <fcntl.h>
12 #include <getopt.h>
13 #include <math.h>
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
19 #include "edid-decode.h"
21 #define STR(x) #x
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;
29 enum output_format {
30 OUT_FMT_DEFAULT,
31 OUT_FMT_HEX,
32 OUT_FMT_RAW,
33 OUT_FMT_CARRAY,
34 OUT_FMT_XML,
38 * Options
39 * Please keep in alphabetical order of the short option.
40 * That makes it easier to see which options are still free.
42 enum Option {
43 OptCheck = 'c',
44 OptCheckInline = 'C',
45 OptFBModeTimings = 'F',
46 OptHelp = 'h',
47 OptOnlyHexDump = 'H',
48 OptLongTimings = 'L',
49 OptNativeResolution = 'n',
50 OptNTSC = 'N',
51 OptOutputFormat = 'o',
52 OptPreferredTimings = 'p',
53 OptPhysicalAddress = 'P',
54 OptSkipHexDump = 's',
55 OptShortTimings = 'S',
56 OptV4L2Timings = 'V',
57 OptXModeLineTimings = 'X',
58 OptSkipSHA = 128,
59 OptHideSerialNumbers,
60 OptVersion,
61 OptDiag,
62 OptSTD,
63 OptDMT,
64 OptVIC,
65 OptHDMIVIC,
66 OptCVT,
67 OptGTF,
68 OptOVT,
69 OptListEstTimings,
70 OptListDMTs,
71 OptListVICs,
72 OptListHDMIVICs,
73 OptListRIDTimings,
74 OptListRIDs,
75 OptLast = 256
78 static char options[OptLast];
80 #ifndef __EMSCRIPTEN__
81 static struct option long_options[] = {
82 { "help", no_argument, 0, OptHelp },
83 { "output-format", required_argument, 0, OptOutputFormat },
84 { "native-resolution", no_argument, 0, OptNativeResolution },
85 { "preferred-timings", no_argument, 0, OptPreferredTimings },
86 { "physical-address", no_argument, 0, OptPhysicalAddress },
87 { "skip-hex-dump", no_argument, 0, OptSkipHexDump },
88 { "only-hex-dump", no_argument, 0, OptOnlyHexDump },
89 { "skip-sha", no_argument, 0, OptSkipSHA },
90 { "hide-serial-numbers", no_argument, 0, OptHideSerialNumbers },
91 { "version", no_argument, 0, OptVersion },
92 { "check-inline", no_argument, 0, OptCheckInline },
93 { "check", no_argument, 0, OptCheck },
94 { "short-timings", no_argument, 0, OptShortTimings },
95 { "long-timings", no_argument, 0, OptLongTimings },
96 { "ntsc", no_argument, 0, OptNTSC },
97 { "xmodeline", no_argument, 0, OptXModeLineTimings },
98 { "fbmode", no_argument, 0, OptFBModeTimings },
99 { "v4l2-timings", no_argument, 0, OptV4L2Timings },
100 { "diagonal", required_argument, 0, OptDiag },
101 { "std", required_argument, 0, OptSTD },
102 { "dmt", required_argument, 0, OptDMT },
103 { "vic", required_argument, 0, OptVIC },
104 { "hdmi-vic", required_argument, 0, OptHDMIVIC },
105 { "cvt", required_argument, 0, OptCVT },
106 { "gtf", required_argument, 0, OptGTF },
107 { "ovt", required_argument, 0, OptOVT },
108 { "list-established-timings", no_argument, 0, OptListEstTimings },
109 { "list-dmts", no_argument, 0, OptListDMTs },
110 { "list-vics", no_argument, 0, OptListVICs },
111 { "list-hdmi-vics", no_argument, 0, OptListHDMIVICs },
112 { "list-rid-timings", required_argument, 0, OptListRIDTimings },
113 { "list-rids", no_argument, 0, OptListRIDs },
114 { 0, 0, 0, 0 }
117 static void usage(void)
119 printf("Usage: edid-decode <options> [in [out]]\n"
120 " [in] EDID file to parse. Read from standard input if none given\n"
121 " or if the input filename is '-'.\n"
122 " [out] Output the read EDID to this file. Write to standard output\n"
123 " if the output filename is '-'.\n"
124 "\nOptions:\n"
125 " -o, --output-format <fmt>\n"
126 " If [out] is specified, then write the EDID in this format.\n"
127 " <fmt> is one of:\n"
128 " hex: hex numbers in ascii text (default for stdout)\n"
129 " raw: binary data (default unless writing to stdout)\n"
130 " carray: c-program struct\n"
131 " xml: XML data\n"
132 " -c, --check Check if the EDID conforms to the standards, failures and\n"
133 " warnings are reported at the end.\n"
134 " -C, --check-inline Check if the EDID conforms to the standards, failures and\n"
135 " warnings are reported inline.\n"
136 " -n, --native-resolution Report the native resolution.\n"
137 " -p, --preferred-timings Report the preferred timings.\n"
138 " -P, --physical-address Only report the CEC physical address.\n"
139 " -S, --short-timings Report all video timings in a short format.\n"
140 " -L, --long-timings Report all video timings in a long format.\n"
141 " -N, --ntsc Report the video timings suitable for NTSC-based video.\n"
142 " -X, --xmodeline Report all long video timings in Xorg.conf format.\n"
143 " -F, --fbmode Report all long video timings in fb.modes format.\n"
144 " -V, --v4l2-timings Report all long video timings in v4l2-dv-timings.h format.\n"
145 " -s, --skip-hex-dump Skip the initial hex dump of the EDID.\n"
146 " -H, --only-hex-dump Only output the hex dump of the EDID.\n"
147 " --skip-sha Skip the SHA report.\n"
148 " --hide-serial-numbers Replace serial numbers with '...'.\n"
149 " --version Show the edid-decode version (SHA).\n"
150 " --diagonal <inches> Set the display's diagonal in inches.\n"
151 " --std <byte1>,<byte2> Show the standard timing represented by these two bytes.\n"
152 " --dmt <dmt> Show the timings for the DMT with the given DMT ID.\n"
153 " --vic <vic> Show the timings for this VIC.\n"
154 " --hdmi-vic <hdmivic> Show the timings for this HDMI VIC.\n"
155 " --cvt w=<width>,h=<height>,fps=<fps>[,rb=<rb>][,interlaced][,overscan][,alt][,hblank=<hblank>][,vblank=<vblank>][,early-vsync]\n"
156 " Calculate the CVT timings for the given format.\n"
157 " <fps> is frames per second for progressive timings,\n"
158 " or fields per second for interlaced timings.\n"
159 " <rb> can be 0 (no reduced blanking, default), or\n"
160 " 1-3 for the reduced blanking version.\n"
161 " If 'interlaced' is given, then this is an interlaced format.\n"
162 " If 'overscan' is given, then this is an overscanned format.\n"
163 " If 'alt' is given and <rb>=2, then report the timings\n"
164 " optimized for video: 1000 / 1001 * <fps>.\n"
165 " If 'alt' is given and <rb>=3, then the horizontal blanking\n"
166 " is 160 instead of 80 pixels.\n"
167 " If 'hblank' is given and <rb>=3, then the horizontal blanking\n"
168 " is <hblank> pixels (range of 80-200), overriding 'alt'.\n"
169 " If 'vblank' is given and <rb>=3, then the vertical blanking\n"
170 " time is <vblank> microseconds (range of 460-705).\n"
171 " If 'early-vsync' is given and <rb=3>, then select early vsync.\n"
172 " --gtf w=<width>,h=<height>[,fps=<fps>][,horfreq=<horfreq>][,pixclk=<pixclk>][,interlaced]\n"
173 " [,overscan][,secondary][,C=<c>][,M=<m>][,K=<k>][,J=<j>]\n"
174 " Calculate the GTF timings for the given format.\n"
175 " <fps> is frames per second for progressive timings,\n"
176 " or fields per second for interlaced timings.\n"
177 " <horfreq> is the horizontal frequency in kHz.\n"
178 " <pixclk> is the pixel clock frequency in MHz.\n"
179 " Only one of fps, horfreq or pixclk must be given.\n"
180 " If 'interlaced' is given, then this is an interlaced format.\n"
181 " If 'overscan' is given, then this is an overscanned format.\n"
182 " If 'secondary' is given, then the secondary GTF is used for\n"
183 " reduced blanking, where <c>, <m>, <k> and <j> are parameters\n"
184 " for the secondary curve.\n"
185 " --ovt (rid=<rid>|w=<width>,h=<height>),fps=<fps>\n"
186 " Calculate the OVT timings for the given format.\n"
187 " Either specify a RID or explicitly specify width and height.\n"
188 " --list-established-timings List all known Established Timings.\n"
189 " --list-dmts List all known DMTs.\n"
190 " --list-vics List all known VICs.\n"
191 " --list-hdmi-vics List all known HDMI VICs.\n"
192 " --list-rids List all known RIDs.\n"
193 " --list-rid-timings <rid> List all timings for RID <rid> or all known RIDs if <rid> is 0.\n"
194 " -h, --help Display this help message.\n");
196 #endif
198 static std::string s_msgs[EDID_MAX_BLOCKS + 1][2];
200 void msg(bool is_warn, const char *fmt, ...)
202 char buf[1024] = "";
203 va_list ap;
205 va_start(ap, fmt);
206 vsprintf(buf, fmt, ap);
207 va_end(ap);
209 if (is_warn)
210 state.warnings++;
211 else
212 state.failures++;
213 if (state.data_block.empty())
214 s_msgs[state.block_nr][is_warn] += std::string(" ") + buf;
215 else
216 s_msgs[state.block_nr][is_warn] += " " + state.data_block + ": " + buf;
218 if (options[OptCheckInline])
219 printf("%s: %s", is_warn ? "WARN" : "FAIL", buf);
222 static void show_msgs(bool is_warn)
224 printf("\n%s:\n\n", is_warn ? "Warnings" : "Failures");
225 for (unsigned i = 0; i < state.num_blocks; i++) {
226 if (s_msgs[i][is_warn].empty())
227 continue;
228 printf("Block %u, %s:\n%s",
229 i, block_name(edid[i * EDID_PAGE_SIZE]).c_str(),
230 s_msgs[i][is_warn].c_str());
232 if (s_msgs[EDID_MAX_BLOCKS][is_warn].empty())
233 return;
234 printf("EDID:\n%s",
235 s_msgs[EDID_MAX_BLOCKS][is_warn].c_str());
239 void do_checksum(const char *prefix, const unsigned char *x, size_t len, unsigned unused_bytes)
241 unsigned char check = x[len - 1];
242 unsigned char sum = 0;
243 unsigned i;
245 printf("%sChecksum: 0x%02hhx", prefix, check);
247 for (i = 0; i < len-1; i++)
248 sum += x[i];
250 if ((unsigned char)(check + sum) != 0) {
251 printf(" (should be 0x%02x)", -sum & 0xff);
252 fail("Invalid checksum 0x%02x (should be 0x%02x).\n",
253 check, -sum & 0xff);
255 if (unused_bytes)
256 printf(" Unused space in Extension Block: %u byte%s",
257 unused_bytes, unused_bytes > 1 ? "s" : "");
258 printf("\n");
261 unsigned gcd(unsigned a, unsigned b)
263 while (b) {
264 unsigned t = b;
266 b = a % b;
267 a = t;
269 return a;
272 void calc_ratio(struct timings *t)
274 unsigned d = gcd(t->hact, t->vact);
276 if (d == 0) {
277 t->hratio = t->vratio = 0;
278 return;
280 t->hratio = t->hact / d;
281 t->vratio = t->vact / d;
284 std::string edid_state::dtd_type(unsigned cnt)
286 unsigned len = std::to_string(cta.preparsed_total_dtds).length();
287 char buf[16];
288 sprintf(buf, "DTD %*u", len, cnt);
289 return buf;
292 bool edid_state::match_timings(const timings &t1, const timings &t2)
294 if (t1.hact != t2.hact ||
295 t1.vact != t2.vact ||
296 t1.rb != t2.rb ||
297 t1.interlaced != t2.interlaced ||
298 t1.hfp != t2.hfp ||
299 t1.hbp != t2.hbp ||
300 t1.hsync != t2.hsync ||
301 t1.pos_pol_hsync != t2.pos_pol_hsync ||
302 t1.hratio != t2.hratio ||
303 t1.vfp != t2.vfp ||
304 t1.vbp != t2.vbp ||
305 t1.vsync != t2.vsync ||
306 t1.pos_pol_vsync != t2.pos_pol_vsync ||
307 t1.vratio != t2.vratio ||
308 t1.pixclk_khz != t2.pixclk_khz)
309 return false;
310 return true;
313 static void or_str(std::string &s, const std::string &flag, unsigned &num_flags)
315 if (!num_flags)
316 s = flag;
317 else if (num_flags % 2 == 0)
318 s = s + " | \\\n\t\t" + flag;
319 else
320 s = s + " | " + flag;
321 num_flags++;
325 * Return true if the timings are a close, but not identical,
326 * match. The only differences allowed are polarities and
327 * porches and syncs, provided the total blanking remains the
328 * same.
330 bool timings_close_match(const timings &t1, const timings &t2)
332 // We don't want to deal with borders, you're on your own
333 // if you are using those.
334 if (t1.hborder || t1.vborder ||
335 t2.hborder || t2.vborder)
336 return false;
337 if (t1.hact != t2.hact || t1.vact != t2.vact ||
338 t1.interlaced != t2.interlaced ||
339 t1.pixclk_khz != t2.pixclk_khz ||
340 t1.hfp + t1.hsync + t1.hbp != t2.hfp + t2.hsync + t2.hbp ||
341 t1.vfp + t1.vsync + t1.vbp != t2.vfp + t2.vsync + t2.vbp)
342 return false;
343 if (t1.hfp == t2.hfp &&
344 t1.hsync == t2.hsync &&
345 t1.hbp == t2.hbp &&
346 t1.pos_pol_hsync == t2.pos_pol_hsync &&
347 t1.vfp == t2.vfp &&
348 t1.vsync == t2.vsync &&
349 t1.vbp == t2.vbp &&
350 t1.pos_pol_vsync == t2.pos_pol_vsync)
351 return false;
352 return true;
355 static void print_modeline(unsigned indent, const struct timings *t, double refresh)
357 unsigned offset = (!t->even_vtotal && t->interlaced) ? 1 : 0;
358 unsigned hfp = t->hborder + t->hfp;
359 unsigned hbp = t->hborder + t->hbp;
360 unsigned vfp = t->vborder + t->vfp;
361 unsigned vbp = t->vborder + t->vbp;
363 printf("%*sModeline \"%ux%u_%.2f%s\" %.3f %u %u %u %u %u %u %u %u %cHSync",
364 indent, "",
365 t->hact, t->vact, refresh,
366 t->interlaced ? "i" : "", t->pixclk_khz / 1000.0,
367 t->hact, t->hact + hfp, t->hact + hfp + t->hsync,
368 t->hact + hfp + t->hsync + hbp,
369 t->vact, t->vact + vfp, t->vact + vfp + t->vsync,
370 t->vact + vfp + t->vsync + vbp + offset,
371 t->pos_pol_hsync ? '+' : '-');
372 if (!t->no_pol_vsync)
373 printf(" %cVSync", t->pos_pol_vsync ? '+' : '-');
374 if (t->interlaced)
375 printf(" Interlace");
376 printf("\n");
379 static void print_fbmode(unsigned indent, const struct timings *t,
380 double refresh, double hor_freq_khz)
382 printf("%*smode \"%ux%u-%u%s\"\n",
383 indent, "",
384 t->hact, t->vact,
385 (unsigned)(0.5 + (t->interlaced ? refresh / 2.0 : refresh)),
386 t->interlaced ? "-lace" : "");
387 printf("%*s# D: %.2f MHz, H: %.3f kHz, V: %.2f Hz\n",
388 indent + 8, "",
389 t->pixclk_khz / 1000.0, hor_freq_khz, refresh);
390 printf("%*sgeometry %u %u %u %u 32\n",
391 indent + 8, "",
392 t->hact, t->vact, t->hact, t->vact);
393 unsigned mult = t->interlaced ? 2 : 1;
394 unsigned offset = !t->even_vtotal && t->interlaced;
395 unsigned hfp = t->hborder + t->hfp;
396 unsigned hbp = t->hborder + t->hbp;
397 unsigned vfp = t->vborder + t->vfp;
398 unsigned vbp = t->vborder + t->vbp;
399 printf("%*stimings %llu %d %d %d %u %u %u\n",
400 indent + 8, "",
401 (unsigned long long)(1000000000.0 / (double)(t->pixclk_khz) + 0.5),
402 hbp, hfp, mult * vbp, mult * vfp + offset, t->hsync, mult * t->vsync);
403 if (t->interlaced)
404 printf("%*slaced true\n", indent + 8, "");
405 if (t->pos_pol_hsync)
406 printf("%*shsync high\n", indent + 8, "");
407 if (t->pos_pol_vsync)
408 printf("%*svsync high\n", indent + 8, "");
409 printf("%*sendmode\n", indent, "");
412 static void print_v4l2_timing(const struct timings *t,
413 double refresh, const char *type)
415 printf("\t#define V4L2_DV_BT_%uX%u%c%u_%02u { \\\n",
416 t->hact, t->vact, t->interlaced ? 'I' : 'P',
417 (unsigned)refresh, (unsigned)(0.5 + 100.0 * (refresh - (unsigned)refresh)));
418 printf("\t\t.type = V4L2_DV_BT_656_1120, \\\n");
419 printf("\t\tV4L2_INIT_BT_TIMINGS(%u, %u, %u, ",
420 t->hact, t->vact, t->interlaced);
421 if (!t->pos_pol_hsync && !t->pos_pol_vsync)
422 printf("0, \\\n");
423 else if (t->pos_pol_hsync && t->pos_pol_vsync)
424 printf("\\\n\t\t\tV4L2_DV_HSYNC_POS_POL | V4L2_DV_VSYNC_POS_POL, \\\n");
425 else if (t->pos_pol_hsync)
426 printf("V4L2_DV_HSYNC_POS_POL, \\\n");
427 else
428 printf("V4L2_DV_VSYNC_POS_POL, \\\n");
429 unsigned hfp = t->hborder + t->hfp;
430 unsigned hbp = t->hborder + t->hbp;
431 unsigned vfp = t->vborder + t->vfp;
432 unsigned vbp = t->vborder + t->vbp;
433 printf("\t\t\t%lluULL, %d, %u, %d, %u, %u, %d, %u, %u, %d, \\\n",
434 t->pixclk_khz * 1000ULL, hfp, t->hsync, hbp,
435 vfp, t->vsync, vbp,
436 t->interlaced ? vfp : 0,
437 t->interlaced ? t->vsync : 0,
438 t->interlaced ? vbp + !t->even_vtotal : 0);
440 std::string flags;
441 unsigned num_flags = 0;
442 unsigned vic = 0;
443 unsigned hdmi_vic = 0;
444 const char *std = "0";
446 if (t->interlaced && !t->even_vtotal)
447 or_str(flags, "V4L2_DV_FL_HALF_LINE", num_flags);
448 if (!memcmp(type, "VIC", 3)) {
449 or_str(flags, "V4L2_DV_FL_HAS_CEA861_VIC", num_flags);
450 or_str(flags, "V4L2_DV_FL_IS_CE_VIDEO", num_flags);
451 vic = strtoul(type + 4, 0, 0);
453 if (!memcmp(type, "HDMI VIC", 8)) {
454 or_str(flags, "V4L2_DV_FL_HAS_HDMI_VIC", num_flags);
455 or_str(flags, "V4L2_DV_FL_IS_CE_VIDEO", num_flags);
456 hdmi_vic = strtoul(type + 9, 0, 0);
457 vic = hdmi_vic_to_vic(hdmi_vic);
458 if (vic)
459 or_str(flags, "V4L2_DV_FL_HAS_CEA861_VIC", num_flags);
461 if (vic && (fmod(refresh, 6)) == 0.0)
462 or_str(flags, "V4L2_DV_FL_CAN_REDUCE_FPS", num_flags);
463 if (t->rb)
464 or_str(flags, "V4L2_DV_FL_REDUCED_BLANKING", num_flags);
465 if (t->hratio && t->vratio)
466 or_str(flags, "V4L2_DV_FL_HAS_PICTURE_ASPECT", num_flags);
468 if (!memcmp(type, "VIC", 3) || !memcmp(type, "HDMI VIC", 8))
469 std = "V4L2_DV_BT_STD_CEA861";
470 else if (!memcmp(type, "DMT", 3))
471 std = "V4L2_DV_BT_STD_DMT";
472 else if (!memcmp(type, "CVT", 3))
473 std = "V4L2_DV_BT_STD_CVT";
474 else if (!memcmp(type, "GTF", 3))
475 std = "V4L2_DV_BT_STD_GTF";
476 printf("\t\t\t%s, \\\n", std);
477 printf("\t\t\t%s, \\\n", flags.empty() ? "0" : flags.c_str());
478 printf("\t\t\t{ %u, %u }, %u, %u) \\\n",
479 t->hratio, t->vratio, vic, hdmi_vic);
480 printf("\t}\n");
483 static void print_detailed_timing(unsigned indent, const struct timings *t)
485 printf("%*sHfront %4d Hsync %3u Hback %4d Hpol %s",
486 indent, "",
487 t->hfp, t->hsync, t->hbp, t->pos_pol_hsync ? "P" : "N");
488 if (t->hborder)
489 printf(" Hborder %u", t->hborder);
490 printf("\n");
492 printf("%*sVfront %4u Vsync %3u Vback %4d",
493 indent, "", t->vfp, t->vsync, t->vbp);
494 if (!t->no_pol_vsync)
495 printf(" Vpol %s", t->pos_pol_vsync ? "P" : "N");
496 if (t->vborder)
497 printf(" Vborder %u", t->vborder);
498 if (t->even_vtotal) {
499 printf(" Both Fields");
500 } else if (t->interlaced) {
501 printf(" Vfront +0.5 Odd Field\n");
502 printf("%*sVfront %4d Vsync %3u Vback %4d",
503 indent, "", t->vfp, t->vsync, t->vbp);
504 if (!t->no_pol_vsync)
505 printf(" Vpol %s", t->pos_pol_vsync ? "P" : "N");
506 if (t->vborder)
507 printf(" Vborder %u", t->vborder);
508 printf(" Vback +0.5 Even Field");
510 printf("\n");
513 bool edid_state::print_timings(const char *prefix, const struct timings *t,
514 const char *type, const char *flags,
515 bool detailed, bool do_checks)
517 if (!t) {
518 // Should not happen
519 if (do_checks)
520 fail("Unknown video timings.\n");
521 return false;
524 if (detailed && options[OptShortTimings])
525 detailed = false;
526 if (options[OptLongTimings])
527 detailed = true;
529 unsigned vact = t->vact;
530 unsigned hbl = t->hfp + t->hsync + t->hbp + 2 * t->hborder;
531 unsigned vbl = t->vfp + t->vsync + t->vbp + 2 * t->vborder;
532 unsigned htotal = t->hact + hbl;
533 double hor_freq_khz = htotal ? (double)t->pixclk_khz / htotal : 0;
535 if (t->interlaced)
536 vact /= 2;
538 double out_hor_freq_khz = hor_freq_khz;
539 if (t->ycbcr420)
540 hor_freq_khz /= 2;
542 double vtotal = vact + vbl;
544 bool ok = true;
546 if (!t->hact || !hbl || !t->hfp || !t->hsync ||
547 !vact || !vbl || (!t->vfp && !t->interlaced && !t->even_vtotal) || !t->vsync) {
548 if (do_checks)
549 fail("0 values in the video timing:\n"
550 " Horizontal Active/Blanking %u/%u\n"
551 " Horizontal Frontporch/Sync Width %u/%u\n"
552 " Vertical Active/Blanking %u/%u\n"
553 " Vertical Frontporch/Sync Width %u/%u\n",
554 t->hact, hbl, t->hfp, t->hsync, vact, vbl, t->vfp, t->vsync);
555 ok = false;
558 if (t->even_vtotal)
559 vtotal = vact + t->vfp + t->vsync + t->vbp;
560 else if (t->interlaced)
561 vtotal = vact + t->vfp + t->vsync + t->vbp + 0.5;
563 double refresh = t->pixclk_khz * 1000.0 / (htotal * vtotal);
564 double pixclk = t->pixclk_khz * 1000.0;
565 if (options[OptNTSC] && fmod(refresh, 6.0) == 0) {
566 const double ntsc_fact = 1000.0 / 1001.0;
567 pixclk *= ntsc_fact;
568 refresh *= ntsc_fact;
569 out_hor_freq_khz *= ntsc_fact;
572 std::string s;
573 unsigned rb = t->rb & ~RB_ALT;
574 if (rb) {
575 bool alt = t->rb & RB_ALT;
576 s = "RB";
577 if (rb == RB_CVT_V2)
578 s += std::string("v2") + (alt ? ",video-optimized" : "");
579 else if (rb == RB_CVT_V3)
580 s += std::string("v3") + (alt ? ",h-blank-160" : "");
582 add_str(s, flags);
583 if (t->hsize_mm || t->vsize_mm)
584 add_str(s, std::to_string(t->hsize_mm) + " mm x " + std::to_string(t->vsize_mm) + " mm");
585 if (t->hsize_mm > dtd_max_hsize_mm)
586 dtd_max_hsize_mm = t->hsize_mm;
587 if (t->vsize_mm > dtd_max_vsize_mm)
588 dtd_max_vsize_mm = t->vsize_mm;
589 if (!s.empty())
590 s = " (" + s + ")";
591 unsigned pixclk_khz = t->pixclk_khz / (t->ycbcr420 ? 2 : 1);
593 char buf[10];
595 sprintf(buf, "%u%s", t->vact, t->interlaced ? "i" : "");
596 printf("%s%s: %5ux%-5s %10.6f Hz %3u:%-3u %8.3f kHz %13.6f MHz%s\n",
597 prefix, type,
598 t->hact, buf,
599 refresh,
600 t->hratio, t->vratio,
601 out_hor_freq_khz,
602 pixclk / 1000000.0,
603 s.c_str());
605 unsigned len = strlen(prefix) + 2;
607 if (!t->ycbcr420 && detailed && options[OptXModeLineTimings])
608 print_modeline(len, t, refresh);
609 else if (!t->ycbcr420 && detailed && options[OptFBModeTimings])
610 print_fbmode(len, t, refresh, hor_freq_khz);
611 else if (!t->ycbcr420 && detailed && options[OptV4L2Timings])
612 print_v4l2_timing(t, refresh, type);
613 else if (detailed)
614 print_detailed_timing(len + strlen(type) + 6, t);
616 if (!do_checks)
617 return ok;
619 if (!memcmp(type, "DTD", 3)) {
620 unsigned vic, dmt;
621 const timings *vic_t = cta_close_match_to_vic(*t, vic);
623 if (vic_t)
624 warn("DTD is similar but not identical to VIC %u.\n", vic);
626 const timings *dmt_t = close_match_to_dmt(*t, dmt);
627 if (!vic_t && dmt_t)
628 warn("DTD is similar but not identical to DMT 0x%02x.\n", dmt);
631 if (refresh) {
632 min_vert_freq_hz = min(min_vert_freq_hz, refresh);
633 max_vert_freq_hz = max(max_vert_freq_hz, refresh);
635 if (hor_freq_khz) {
636 min_hor_freq_hz = min(min_hor_freq_hz, hor_freq_khz * 1000.0);
637 max_hor_freq_hz = max(max_hor_freq_hz, hor_freq_khz * 1000.0);
638 max_pixclk_khz = max(max_pixclk_khz, pixclk_khz);
639 if (t->pos_pol_hsync && !t->pos_pol_vsync && t->vsync == 3)
640 base.max_pos_neg_hor_freq_khz = hor_freq_khz;
643 if (t->ycbcr420 && t->pixclk_khz < 590000)
644 warn_once("Some YCbCr 4:2:0 timings are invalid for HDMI 2.1 (which requires an RGB timings pixel rate >= 590 MHz).\n");
645 if (t->hfp <= 0)
646 fail("0 or negative horizontal front porch.\n");
647 if (t->hbp <= 0)
648 fail("0 or negative horizontal back porch.\n");
649 if (t->vbp <= 0)
650 fail("0 or negative vertical back porch.\n");
651 if (!base.max_display_width_mm && !base.max_display_height_mm) {
652 /* this is valid */
653 } else if (!t->hsize_mm && !t->vsize_mm) {
654 /* this is valid */
655 } else if (t->hsize_mm > base.max_display_width_mm + 9 ||
656 t->vsize_mm > base.max_display_height_mm + 9) {
657 fail("Mismatch of image size %ux%u mm vs display size %ux%u mm.\n",
658 t->hsize_mm, t->vsize_mm, base.max_display_width_mm, base.max_display_height_mm);
659 } else if (t->hsize_mm < base.max_display_width_mm - 9 &&
660 t->vsize_mm < base.max_display_height_mm - 9) {
661 fail("Mismatch of image size %ux%u mm vs display size %ux%u mm.\n",
662 t->hsize_mm, t->vsize_mm, base.max_display_width_mm, base.max_display_height_mm);
664 return ok;
667 std::string containerid2s(const unsigned char *x)
669 char buf[40];
671 sprintf(buf, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
672 x[0], x[1], x[2], x[3],
673 x[4], x[5],
674 x[6], x[7],
675 x[8], x[9],
676 x[10], x[11], x[12], x[13], x[14], x[15]);
677 return buf;
680 std::string utohex(unsigned char x)
682 char buf[10];
684 sprintf(buf, "0x%02hhx", x);
685 return buf;
688 const char *oui_name(unsigned oui, unsigned *ouinum)
690 unsigned ouinumscratch;
691 if (!ouinum) ouinum = &ouinumscratch;
692 const char *name;
693 switch (oui) {
694 #define oneoui(c,k,n) case c: *ouinum = kOUI_##k; name = n; break;
695 #include "oui.h"
696 default: *ouinum = 0; name = NULL; break;
698 return name;
701 void edid_state::data_block_oui(std::string block_name, const unsigned char *x,
702 unsigned length, unsigned *ouinum, bool ignorezeros, bool do_ascii, bool big_endian)
704 std::string buf;
705 char ascii[4];
706 unsigned oui;
707 const char *ouiname = NULL;
708 bool matched_reverse = false;
709 bool matched_ascii = false;
710 bool valid_ascii = false;
712 if (big_endian)
713 oui = ((length > 0 ? x[0] : 0) << 16) + ((length > 1 ? x[1] : 0) << 8) + (length > 2 ? x[2] : 0);
714 else
715 oui = ((length > 2 ? x[2] : 0) << 16) + ((length > 1 ? x[1] : 0) << 8) + (length > 0 ? x[0] : 0);
717 buf = ouitohex(oui);
718 if (length < 3) {
719 sprintf(ascii, "?"); // some characters are null
720 if (ouinum) *ouinum = 0; // doesn't match a known OUI
721 } else {
722 valid_ascii = (x[0] >= 'A' && x[1] >= 'A' && x[2] >= 'A' && x[0] <= 'Z' && x[1] <= 'Z' && x[2] <= 'Z');
723 sprintf(ascii, "%c%c%c", x[0], x[1], x[2]);
725 ouiname = oui_name(oui, ouinum);
726 if (!ouiname) {
727 big_endian = !big_endian;
728 unsigned reversedoui = ((oui & 0xff) << 16) + (oui & 0x00ff00) + (oui >> 16);
729 ouiname = oui_name(reversedoui, ouinum);
730 if (ouiname) {
731 oui = reversedoui;
732 buf = ouitohex(oui);
733 matched_reverse = true;
734 } else if (do_ascii && valid_ascii) {
735 unsigned asciioui = (x[0] << 24) + (x[1] << 16) + (x[2] << 8);
736 ouiname = oui_name(asciioui, ouinum);
737 if (ouiname) {
738 matched_ascii = true;
744 std::string name;
745 if (ouiname) {
746 if (matched_ascii)
747 name = block_name + " (" + ouiname + ")" + ", PNP ID '" + ascii + "'";
748 else
749 name = block_name + " (" + ouiname + ")" + ", OUI " + buf;
750 } else if (do_ascii && valid_ascii) {
751 name = block_name + ", PNP ID '" + ascii + "'";
752 } else {
753 name = block_name + ", OUI " + buf;
755 // assign string to data_block before outputting errors
756 data_block = name;
758 if (oui || !ignorezeros) {
759 printf(" %s:\n", data_block.c_str());
760 if (length < 3)
761 fail("Data block length (%d) is not enough to contain an OUI.\n", length);
762 else if (ouiname) {
763 if (do_ascii && !valid_ascii)
764 warn("Expected PNP ID but found OUI.\n");
765 if (matched_reverse)
766 fail("Endian-ness (%s) of OUI is different than expected (%s).\n", big_endian ? "be" : "le", big_endian ? "le" : "be");
768 else {
769 if (valid_ascii)
770 warn("Unknown OUI %s (possible PNP %s).\n", buf.c_str(), ascii);
771 else
772 warn("Unknown OUI %s.\n", buf.c_str());
777 std::string ouitohex(unsigned oui)
779 char buf[32];
781 sprintf(buf, "%02X-%02X-%02X", (oui >> 16) & 0xff, (oui >> 8) & 0xff, oui & 0xff);
782 return buf;
785 bool memchk(const unsigned char *x, unsigned len, unsigned char v)
787 for (unsigned i = 0; i < len; i++)
788 if (x[i] != v)
789 return false;
790 return true;
793 void hex_block(const char *prefix, const unsigned char *x,
794 unsigned length, bool show_ascii, unsigned step)
796 unsigned i, j;
798 for (i = 0; i < length; i += step) {
799 unsigned len = min(step, length - i);
801 printf("%s", prefix);
802 for (j = 0; j < len; j++)
803 printf("%s%02x", j ? " " : "", x[i + j]);
805 if (show_ascii) {
806 for (j = len; j < step; j++)
807 printf(" ");
808 printf(" '");
809 for (j = 0; j < len; j++)
810 printf("%c", x[i + j] >= ' ' && x[i + j] <= '~' ? x[i + j] : '.');
811 printf("'");
813 printf("\n");
817 static bool edid_add_byte(const char *s, bool two_digits = true)
819 char buf[3];
821 if (state.edid_size == sizeof(edid))
822 return false;
823 buf[0] = s[0];
824 buf[1] = two_digits ? s[1] : 0;
825 buf[2] = 0;
826 edid[state.edid_size++] = strtoul(buf, NULL, 16);
827 return true;
830 static bool extract_edid_quantumdata(const char *start)
832 /* Parse QuantumData 980 EDID files */
833 do {
834 start = strstr(start, ">");
835 if (!start)
836 return false;
837 start++;
838 for (unsigned i = 0; start[i] && start[i + 1] && i < 256; i += 2)
839 if (!edid_add_byte(start + i))
840 return false;
841 start = strstr(start, "<BLOCK");
842 } while (start);
843 return state.edid_size;
846 static const char *ignore_chars = ",:;";
848 static bool extract_edid_hex(const char *s, bool require_two_digits = true)
850 for (; *s; s++) {
851 if (isspace(*s) || strchr(ignore_chars, *s))
852 continue;
854 if (*s == '0' && tolower(s[1]) == 'x') {
855 s++;
856 continue;
859 /* Read one or two hex digits from the log */
860 if (!isxdigit(s[0])) {
861 if (state.edid_size && state.edid_size % 128 == 0)
862 break;
863 return false;
865 if (require_two_digits && !isxdigit(s[1])) {
866 odd_hex_digits = true;
867 return false;
869 if (!edid_add_byte(s, isxdigit(s[1])))
870 return false;
871 if (isxdigit(s[1]))
872 s++;
874 return state.edid_size;
877 static bool extract_edid_xrandr(const char *start)
879 static const char indentation1[] = " ";
880 static const char indentation2[] = "\t\t";
881 /* Used to detect that we've gone past the EDID property */
882 static const char half_indentation1[] = " ";
883 static const char half_indentation2[] = "\t";
884 const char *indentation;
885 const char *s;
887 for (;;) {
888 unsigned j;
890 /* Get the next start of the line of EDID hex, assuming spaces for indentation */
891 s = strstr(start, indentation = indentation1);
892 /* Did we skip the start of another property? */
893 if (s && s > strstr(start, half_indentation1))
894 break;
896 /* If we failed, retry assuming tabs for indentation */
897 if (!s) {
898 s = strstr(start, indentation = indentation2);
899 /* Did we skip the start of another property? */
900 if (s && s > strstr(start, half_indentation2))
901 break;
904 if (!s)
905 break;
907 start = s + strlen(indentation);
909 for (j = 0; j < 16; j++, start += 2) {
910 /* Read a %02x from the log */
911 if (!isxdigit(start[0]) || !isxdigit(start[1])) {
912 if (j)
913 break;
914 return false;
916 if (!edid_add_byte(start))
917 return false;
920 return state.edid_size;
923 static bool extract_edid_xorg(const char *start)
925 bool find_first_num = true;
927 for (; *start; start++) {
928 if (find_first_num) {
929 const char *s;
931 /* skip ahead to the : */
932 s = strstr(start, ": \t");
933 if (!s)
934 s = strstr(start, ": ");
935 if (!s)
936 break;
937 start = s;
938 /* and find the first number */
939 while (!isxdigit(start[1]))
940 start++;
941 find_first_num = false;
942 continue;
943 } else {
944 /* Read a %02x from the log */
945 if (!isxdigit(*start)) {
946 find_first_num = true;
947 continue;
949 if (!edid_add_byte(start))
950 return false;
951 start++;
954 return state.edid_size;
957 static bool extract_edid(int fd, FILE *error)
959 std::vector<char> edid_data;
960 char buf[EDID_PAGE_SIZE];
962 for (;;) {
963 ssize_t i = read(fd, buf, sizeof(buf));
965 if (i < 0)
966 return false;
967 if (i == 0)
968 break;
969 edid_data.insert(edid_data.end(), buf, buf + i);
972 if (edid_data.empty()) {
973 state.edid_size = 0;
974 return false;
977 const char *data = &edid_data[0];
978 const char *start;
980 /* Look for edid-decode output */
981 start = strstr(data, "EDID (hex):");
982 if (!start)
983 start = strstr(data, "edid-decode (hex):");
984 if (start)
985 return extract_edid_hex(strchr(start, ':'));
987 /* Look for C-array */
988 start = strstr(data, "unsigned char edid[] = {");
989 if (start)
990 return extract_edid_hex(strchr(start, '{') + 1, false);
992 /* Look for QuantumData EDID output */
993 start = strstr(data, "<BLOCK");
994 if (start)
995 return extract_edid_quantumdata(start);
997 /* Look for xrandr --verbose output (lines of 16 hex bytes) */
998 start = strstr(data, "EDID_DATA:");
999 if (!start)
1000 start = strstr(data, "EDID:");
1001 if (start)
1002 return extract_edid_xrandr(start);
1004 /* Look for an EDID in an Xorg.0.log file */
1005 start = strstr(data, "EDID (in hex):");
1006 if (start)
1007 start = strstr(start, "(II)");
1008 if (start)
1009 return extract_edid_xorg(start);
1011 unsigned i;
1013 /* Is the EDID provided in hex? */
1014 for (i = 0; i < 32 && (isspace(data[i]) || strchr(ignore_chars, data[i]) ||
1015 tolower(data[i]) == 'x' || isxdigit(data[i])); i++);
1017 if (i == 32)
1018 return extract_edid_hex(data);
1020 /* Assume binary */
1021 if (edid_data.size() > sizeof(edid)) {
1022 fprintf(error, "Binary EDID length %zu is greater than %zu.\n",
1023 edid_data.size(), sizeof(edid));
1024 return false;
1026 memcpy(edid, data, edid_data.size());
1027 state.edid_size = edid_data.size();
1028 return true;
1031 static int edid_from_file(const char *from_file, FILE *error)
1033 #ifdef O_BINARY
1034 // Windows compatibility
1035 int flags = O_RDONLY | O_BINARY;
1036 #else
1037 int flags = O_RDONLY;
1038 #endif
1039 int fd;
1041 if (!strcmp(from_file, "-")) {
1042 from_file = "stdin";
1043 fd = 0;
1044 } else if ((fd = open(from_file, flags)) == -1) {
1045 perror(from_file);
1046 return -1;
1049 odd_hex_digits = false;
1050 if (!extract_edid(fd, error)) {
1051 if (!state.edid_size) {
1052 fprintf(error, "EDID of '%s' was empty.\n", from_file);
1053 return -1;
1055 fprintf(error, "EDID extract of '%s' failed: ", from_file);
1056 if (odd_hex_digits)
1057 fprintf(error, "odd number of hexadecimal digits.\n");
1058 else
1059 fprintf(error, "unknown format.\n");
1060 return -1;
1062 if (state.edid_size % EDID_PAGE_SIZE) {
1063 fprintf(error, "EDID length %u is not a multiple of %u.\n",
1064 state.edid_size, EDID_PAGE_SIZE);
1065 return -1;
1067 state.num_blocks = state.edid_size / EDID_PAGE_SIZE;
1068 if (fd != 0)
1069 close(fd);
1071 if (memcmp(edid, "\x00\xFF\xFF\xFF\xFF\xFF\xFF\x00", 8)) {
1072 fprintf(error, "No EDID header found in '%s'.\n", from_file);
1073 return -1;
1075 return 0;
1078 /* generic extension code */
1080 std::string block_name(unsigned char block)
1082 char buf[10];
1084 switch (block) {
1085 case 0x00: return "Base EDID";
1086 case 0x02: return "CTA-861 Extension Block";
1087 case 0x10: return "Video Timing Extension Block";
1088 case 0x20: return "EDID 2.0 Extension Block";
1089 case 0x40: return "Display Information Extension Block";
1090 case 0x50: return "Localized String Extension Block";
1091 case 0x60: return "Microdisplay Interface Extension Block";
1092 case 0x70: return "DisplayID Extension Block";
1093 case 0xf0: return "Block Map Extension Block";
1094 case 0xff: return "Manufacturer-Specific Extension Block";
1095 default:
1096 sprintf(buf, " 0x%02x", block);
1097 return std::string("Unknown EDID Extension Block") + buf;
1101 void edid_state::parse_block_map(const unsigned char *x)
1103 unsigned last_valid_block_tag = 0;
1104 bool fail_once = false;
1105 unsigned offset = 1;
1106 unsigned i;
1108 if (block_nr == 1)
1109 block_map.saw_block_1 = true;
1110 else if (!block_map.saw_block_1)
1111 fail("No EDID Block Map Extension found in block 1.\n");
1112 else if (block_nr == 128)
1113 block_map.saw_block_128 = true;
1115 if (block_nr > 1)
1116 offset = 128;
1118 for (i = 1; i < 127; i++) {
1119 unsigned block = offset + i;
1121 if (x[i]) {
1122 last_valid_block_tag++;
1123 if (i != last_valid_block_tag && !fail_once) {
1124 fail("Valid block tags are not consecutive.\n");
1125 fail_once = true;
1127 printf(" Block %3u: %s\n", block, block_name(x[i]).c_str());
1128 if (block >= num_blocks) {
1129 if (!fail_once)
1130 fail("Invalid block number %u.\n", block);
1131 fail_once = true;
1132 } else if (x[i] != edid[block * EDID_PAGE_SIZE]) {
1133 fail("Block %u tag mismatch: expected 0x%02x, but got 0x%02x.\n",
1134 block, edid[block * EDID_PAGE_SIZE], x[i]);
1136 } else if (block < num_blocks) {
1137 fail("Block %u tag mismatch: expected 0x%02x, but got 0x00.\n",
1138 block, edid[block * EDID_PAGE_SIZE]);
1143 void edid_state::preparse_extension(const unsigned char *x)
1145 switch (x[0]) {
1146 case 0x02:
1147 has_cta = true;
1148 preparse_cta_block(x);
1149 break;
1150 case 0x70:
1151 has_dispid = true;
1152 preparse_displayid_block(x);
1153 break;
1157 void edid_state::parse_extension(const unsigned char *x)
1159 block = block_name(x[0]);
1160 data_block.clear();
1161 unused_bytes = 0;
1163 printf("\n");
1164 if (block_nr && x[0] == 0)
1165 block = "Unknown EDID Extension Block 0x00";
1166 printf("Block %u, %s:\n", block_nr, block.c_str());
1168 switch (x[0]) {
1169 case 0x02:
1170 parse_cta_block(x);
1171 break;
1172 case 0x10:
1173 parse_vtb_ext_block(x);
1174 break;
1175 case 0x20:
1176 fail("Deprecated extension block for EDID 2.0, do not use.\n");
1177 break;
1178 case 0x40:
1179 parse_di_ext_block(x);
1180 break;
1181 case 0x50:
1182 parse_ls_ext_block(x);
1183 break;
1184 case 0x70:
1185 parse_displayid_block(x);
1186 break;
1187 case 0xf0:
1188 parse_block_map(x);
1189 if (block_nr != 1 && block_nr != 128)
1190 fail("Must be used in block 1 and 128.\n");
1191 break;
1192 default:
1193 hex_block(" ", x, EDID_PAGE_SIZE);
1194 fail("Unknown Extension Block.\n");
1195 break;
1198 data_block.clear();
1199 do_checksum("", x, EDID_PAGE_SIZE, unused_bytes);
1202 void edid_state::print_preferred_timings()
1204 if (base.preferred_timing.is_valid()) {
1205 printf("\n----------------\n");
1206 printf("\nPreferred Video Timing if only Block 0 is parsed:\n");
1207 print_timings(" ", base.preferred_timing, true, false);
1210 if (!cta.preferred_timings.empty()) {
1211 printf("\n----------------\n");
1212 printf("\nPreferred Video Timing%s if Block 0 and CTA-861 Blocks are parsed:\n",
1213 cta.preferred_timings.size() > 1 ? "s" : "");
1214 for (vec_timings_ext::iterator iter = cta.preferred_timings.begin();
1215 iter != cta.preferred_timings.end(); ++iter)
1216 print_timings(" ", *iter, true, false);
1219 if (!dispid.preferred_timings.empty()) {
1220 printf("\n----------------\n");
1221 printf("\nPreferred Video Timing%s if Block 0 and DisplayID Blocks are parsed:\n",
1222 dispid.preferred_timings.size() > 1 ? "s" : "");
1223 for (vec_timings_ext::iterator iter = dispid.preferred_timings.begin();
1224 iter != dispid.preferred_timings.end(); ++iter)
1225 print_timings(" ", *iter, true, false);
1229 void edid_state::print_native_res()
1231 typedef std::pair<unsigned, unsigned> resolution;
1232 typedef std::set<resolution> resolution_set;
1233 resolution_set native_prog, native_int;
1234 unsigned native_width = 0, native_height = 0;
1235 unsigned native_width_int = 0, native_height_int = 0;
1237 // Note: it is also a mismatch if Block 0 does not define a
1238 // native resolution, but other blocks do.
1239 bool native_mismatch = false;
1240 bool native_int_mismatch = false;
1242 if (base.preferred_timing.is_valid() && base.preferred_is_also_native) {
1243 if (base.preferred_timing.t.interlaced) {
1244 native_width_int = base.preferred_timing.t.hact;
1245 native_height_int = base.preferred_timing.t.vact;
1246 } else {
1247 native_width = base.preferred_timing.t.hact;
1248 native_height = base.preferred_timing.t.vact;
1252 if (!native_width && dispid.native_width) {
1253 native_width = dispid.native_width;
1254 native_height = dispid.native_height;
1255 native_mismatch = true;
1256 } else if (dispid.native_width && native_width &&
1257 (dispid.native_width != native_width ||
1258 dispid.native_height != native_height)) {
1259 native_mismatch = true;
1262 for (vec_timings_ext::iterator iter = cta.native_timings.begin();
1263 iter != cta.native_timings.end(); ++iter) {
1264 if (iter->t.interlaced) {
1265 native_int.insert(std::pair<unsigned, unsigned>(iter->t.hact, iter->t.vact));
1266 if (!native_width_int) {
1267 native_width_int = iter->t.hact;
1268 native_height_int = iter->t.vact;
1269 native_int_mismatch = true;
1270 } else if (native_width_int &&
1271 (iter->t.hact != native_width_int ||
1272 iter->t.vact != native_height_int)) {
1273 native_int_mismatch = true;
1275 } else {
1276 native_prog.insert(std::pair<unsigned, unsigned>(iter->t.hact, iter->t.vact));
1277 if (!native_width) {
1278 native_width = iter->t.hact;
1279 native_height = iter->t.vact;
1280 native_mismatch = true;
1281 } else if (native_width &&
1282 (iter->t.hact != native_width ||
1283 iter->t.vact != native_height)) {
1284 native_mismatch = true;
1289 if (diagonal) {
1290 if (image_width) {
1291 double w = image_width;
1292 double h = image_height;
1293 double d = sqrt(w * w + h * h) / 254.0;
1295 if (fabs(diagonal - d) >= 0.1)
1296 warn("Specified diagonal is %.1f\", calculated diagonal is %.1f\".\n",
1297 diagonal, d);
1299 if (native_width) {
1300 double w = native_width;
1301 double h = native_height;
1302 double d = diagonal * 254.0;
1303 double c = sqrt((d * d) / (w * w + h * h));
1305 w *= c;
1306 h *= c;
1308 if (image_width) {
1309 if (fabs((double)image_width - w) >= 100.0 ||
1310 fabs((double)image_height - h) >= 100.0)
1311 warn("Calculated image size is %.1fx%.1fmm, EDID image size is %.1fx%.1fmm.\n",
1312 w / 10.0, h / 10.0,
1313 image_width / 10.0, image_height / 10.0);
1314 } else {
1315 warn("No image size was specified, but it is calculated as %.1fx%.1fmm.\n",
1316 w / 10.0, h / 10.0);
1321 if (!options[OptNativeResolution])
1322 return;
1324 if (native_width == 0 && native_width_int == 0) {
1325 printf("\n----------------\n");
1326 printf("\nNo Native Video Resolution was defined.\n");
1327 return;
1330 if ((native_width || native_width_int) &&
1331 !native_mismatch && !native_int_mismatch) {
1332 printf("\n----------------\n");
1333 printf("\nNative Video Resolution%s:\n",
1334 native_width && native_width_int ? "s" : "");
1335 if (native_width)
1336 printf(" %ux%u\n", native_width, native_height);
1337 if (native_width_int)
1338 printf(" %ux%ui\n", native_width_int, native_height_int);
1339 return;
1342 if (base.preferred_timing.is_valid() && base.preferred_is_also_native) {
1343 printf("\n----------------\n");
1344 printf("\nNative Video Resolution if only Block 0 is parsed:\n");
1345 printf(" %ux%u%s\n",
1346 base.preferred_timing.t.hact, base.preferred_timing.t.vact,
1347 base.preferred_timing.t.interlaced ? "i" : "");
1350 if (!cta.native_timings.empty()) {
1351 printf("\n----------------\n");
1352 printf("\nNative Video Resolution%s if Block 0 and CTA-861 Blocks are parsed:\n",
1353 native_prog.size() + native_int.size() > 1 ? "s" : "");
1354 for (resolution_set::iterator iter = native_prog.begin();
1355 iter != native_prog.end(); ++iter)
1356 printf(" %ux%u\n", iter->first, iter->second);
1357 for (resolution_set::iterator iter = native_int.begin();
1358 iter != native_int.end(); ++iter)
1359 printf(" %ux%ui\n", iter->first, iter->second);
1362 if (dispid.native_width) {
1363 printf("\n----------------\n");
1364 printf("\nNative Video Resolution if the DisplayID Blocks are parsed:\n");
1365 printf(" %ux%u\n", dispid.native_width, dispid.native_height);
1369 int edid_state::parse_edid()
1371 hide_serial_numbers = options[OptHideSerialNumbers];
1373 for (unsigned i = 1; i < num_blocks; i++)
1374 preparse_extension(edid + i * EDID_PAGE_SIZE);
1376 if (options[OptPhysicalAddress]) {
1377 printf("%x.%x.%x.%x\n",
1378 (cta.preparsed_phys_addr >> 12) & 0xf,
1379 (cta.preparsed_phys_addr >> 8) & 0xf,
1380 (cta.preparsed_phys_addr >> 4) & 0xf,
1381 cta.preparsed_phys_addr & 0xf);
1382 return 0;
1385 if (!options[OptSkipHexDump]) {
1386 printf("edid-decode (hex):\n\n");
1387 for (unsigned i = 0; i < num_blocks; i++) {
1388 hex_block("", edid + i * EDID_PAGE_SIZE, EDID_PAGE_SIZE, false);
1389 if (i == num_blocks - 1 && options[OptOnlyHexDump])
1390 return 0;
1391 printf("\n");
1393 printf("----------------\n\n");
1396 block = block_name(0x00);
1397 printf("Block %u, %s:\n", block_nr, block.c_str());
1398 parse_base_block(edid);
1400 for (unsigned i = 1; i < num_blocks; i++) {
1401 block_nr++;
1402 printf("\n----------------\n");
1403 parse_extension(edid + i * EDID_PAGE_SIZE);
1406 block = "";
1407 block_nr = EDID_MAX_BLOCKS;
1409 if (has_cta)
1410 cta_resolve_svrs();
1412 if (options[OptPreferredTimings])
1413 print_preferred_timings();
1415 print_native_res();
1417 if (!options[OptCheck] && !options[OptCheckInline])
1418 return 0;
1420 check_base_block();
1421 if (has_cta)
1422 check_cta_blocks();
1423 if (has_dispid)
1424 check_displayid_blocks();
1426 printf("\n----------------\n");
1428 if (!options[OptSkipSHA] && strlen(STRING(SHA))) {
1429 printf("\nedid-decode SHA: %s %s\n", STRING(SHA), STRING(DATE));
1432 if (options[OptCheck]) {
1433 if (warnings)
1434 show_msgs(true);
1435 if (failures)
1436 show_msgs(false);
1438 printf("\nEDID conformity: %s\n", failures ? "FAIL" : "PASS");
1439 return failures ? -2 : 0;
1442 #ifndef __EMSCRIPTEN__
1444 static unsigned char crc_calc(const unsigned char *b)
1446 unsigned char sum = 0;
1447 unsigned i;
1449 for (i = 0; i < 127; i++)
1450 sum += b[i];
1451 return 256 - sum;
1454 static int crc_ok(const unsigned char *b)
1456 return crc_calc(b) == b[127];
1459 static void hexdumpedid(FILE *f, const unsigned char *edid, unsigned size)
1461 unsigned b, i, j;
1463 for (b = 0; b < size / 128; b++) {
1464 const unsigned char *buf = edid + 128 * b;
1466 if (b)
1467 fprintf(f, "\n");
1468 for (i = 0; i < 128; i += 0x10) {
1469 fprintf(f, "%02x", buf[i]);
1470 for (j = 1; j < 0x10; j++) {
1471 fprintf(f, " %02x", buf[i + j]);
1473 fprintf(f, "\n");
1475 if (!crc_ok(buf))
1476 fprintf(f, "Block %u has a checksum error (should be 0x%02x).\n",
1477 b, crc_calc(buf));
1481 static void carraydumpedid(FILE *f, const unsigned char *edid, unsigned size)
1483 unsigned b, i, j;
1485 fprintf(f, "const unsigned char edid[] = {\n");
1486 for (b = 0; b < size / 128; b++) {
1487 const unsigned char *buf = edid + 128 * b;
1489 if (b)
1490 fprintf(f, "\n");
1491 for (i = 0; i < 128; i += 8) {
1492 fprintf(f, "\t0x%02x,", buf[i]);
1493 for (j = 1; j < 8; j++) {
1494 fprintf(f, " 0x%02x,", buf[i + j]);
1496 fprintf(f, "\n");
1498 if (!crc_ok(buf))
1499 fprintf(f, "\t/* Block %u has a checksum error (should be 0x%02x). */\n",
1500 b, crc_calc(buf));
1502 fprintf(f, "};\n");
1505 // This format can be read by the QuantumData EDID editor
1506 static void xmldumpedid(FILE *f, const unsigned char *edid, unsigned size)
1508 fprintf(f, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
1509 fprintf(f, "<DATAOBJ>\n");
1510 fprintf(f, " <HEADER TYPE=\"DID\" VERSION=\"1.0\"/>\n");
1511 fprintf(f, " <DATA>\n");
1512 for (unsigned b = 0; b < size / 128; b++) {
1513 const unsigned char *buf = edid + 128 * b;
1515 fprintf(f, " <BLOCK%u>", b);
1516 for (unsigned i = 0; i < 128; i++)
1517 fprintf(f, "%02X", buf[i]);
1518 fprintf(f, "</BLOCK%u>\n", b);
1520 fprintf(f, " </DATA>\n");
1521 fprintf(f, "</DATAOBJ>\n");
1524 static int edid_to_file(const char *to_file, enum output_format out_fmt)
1526 FILE *out;
1528 if (!strcmp(to_file, "-")) {
1529 to_file = "stdout";
1530 out = stdout;
1531 } else if ((out = fopen(to_file, "w")) == NULL) {
1532 perror(to_file);
1533 return -1;
1535 if (out_fmt == OUT_FMT_DEFAULT)
1536 out_fmt = out == stdout ? OUT_FMT_HEX : OUT_FMT_RAW;
1538 switch (out_fmt) {
1539 default:
1540 case OUT_FMT_HEX:
1541 hexdumpedid(out, edid, state.edid_size);
1542 break;
1543 case OUT_FMT_RAW:
1544 fwrite(edid, state.edid_size, 1, out);
1545 break;
1546 case OUT_FMT_CARRAY:
1547 carraydumpedid(out, edid, state.edid_size);
1548 break;
1549 case OUT_FMT_XML:
1550 xmldumpedid(out, edid, state.edid_size);
1551 break;
1554 if (out != stdout)
1555 fclose(out);
1556 return 0;
1559 enum cvt_opts {
1560 CVT_WIDTH = 0,
1561 CVT_HEIGHT,
1562 CVT_FPS,
1563 CVT_INTERLACED,
1564 CVT_OVERSCAN,
1565 CVT_RB,
1566 CVT_ALT,
1567 CVT_RB_H_BLANK,
1568 CVT_RB_V_BLANK,
1569 CVT_EARLY_VSYNC,
1572 static int parse_cvt_subopt(char **subopt_str, double *value)
1574 int opt;
1575 char *opt_str;
1577 static const char * const subopt_list[] = {
1578 "w",
1579 "h",
1580 "fps",
1581 "interlaced",
1582 "overscan",
1583 "rb",
1584 "alt",
1585 "hblank",
1586 "vblank",
1587 "early-vsync",
1588 nullptr
1591 opt = getsubopt(subopt_str, (char* const*) subopt_list, &opt_str);
1593 if (opt == -1) {
1594 fprintf(stderr, "Invalid suboptions specified.\n");
1595 usage();
1596 std::exit(EXIT_FAILURE);
1598 if (opt_str == nullptr && opt != CVT_INTERLACED && opt != CVT_ALT &&
1599 opt != CVT_OVERSCAN && opt != CVT_EARLY_VSYNC) {
1600 fprintf(stderr, "No value given to suboption <%s>.\n",
1601 subopt_list[opt]);
1602 usage();
1603 std::exit(EXIT_FAILURE);
1606 if (opt_str)
1607 *value = strtod(opt_str, nullptr);
1608 return opt;
1611 static void parse_cvt(char *optarg)
1613 unsigned w = 0, h = 0;
1614 double fps = 0;
1615 unsigned rb = RB_NONE;
1616 unsigned rb_h_blank = 0;
1617 unsigned rb_v_blank = 460;
1618 bool interlaced = false;
1619 bool alt = false;
1620 bool overscan = false;
1621 bool early_vsync = false;
1623 while (*optarg != '\0') {
1624 int opt;
1625 double opt_val;
1627 opt = parse_cvt_subopt(&optarg, &opt_val);
1629 switch (opt) {
1630 case CVT_WIDTH:
1631 w = round(opt_val);
1632 break;
1633 case CVT_HEIGHT:
1634 h = round(opt_val);
1635 break;
1636 case CVT_FPS:
1637 fps = opt_val;
1638 break;
1639 case CVT_RB:
1640 rb = opt_val;
1641 break;
1642 case CVT_OVERSCAN:
1643 overscan = true;
1644 break;
1645 case CVT_INTERLACED:
1646 interlaced = opt_val;
1647 break;
1648 case CVT_ALT:
1649 alt = opt_val;
1650 break;
1651 case CVT_RB_H_BLANK:
1652 rb_h_blank = opt_val;
1653 break;
1654 case CVT_RB_V_BLANK:
1655 rb_v_blank = opt_val;
1656 if (rb_v_blank < 460) {
1657 fprintf(stderr, "vblank must be >= 460, set to 460.\n");
1658 rb_v_blank = 460;
1659 } else if (rb_v_blank > 705) {
1660 fprintf(stderr, "warning: vblank values > 705 might not be supported by RBv3 compliant sources.\n");
1662 break;
1663 case CVT_EARLY_VSYNC:
1664 early_vsync = true;
1665 break;
1666 default:
1667 break;
1671 if (!w || !h || !fps) {
1672 fprintf(stderr, "Missing width, height and/or fps.\n");
1673 usage();
1674 std::exit(EXIT_FAILURE);
1676 if (interlaced)
1677 fps /= 2;
1678 timings t = state.calc_cvt_mode(w, h, fps, rb, interlaced, overscan, alt,
1679 rb_h_blank, rb_v_blank, early_vsync);
1680 state.print_timings("", &t, "CVT", "", true, false);
1683 struct gtf_parsed_data {
1684 unsigned w, h;
1685 double freq;
1686 double C, M, K, J;
1687 bool overscan;
1688 bool interlaced;
1689 bool secondary;
1690 bool params_from_edid;
1691 enum gtf_ip_parm ip_parm;
1694 enum gtf_opts {
1695 GTF_WIDTH = 0,
1696 GTF_HEIGHT,
1697 GTF_FPS,
1698 GTF_HORFREQ,
1699 GTF_PIXCLK,
1700 GTF_INTERLACED,
1701 GTF_OVERSCAN,
1702 GTF_SECONDARY,
1703 GTF_C2,
1704 GTF_M,
1705 GTF_K,
1706 GTF_J2,
1709 static int parse_gtf_subopt(char **subopt_str, double *value)
1711 int opt;
1712 char *opt_str;
1714 static const char * const subopt_list[] = {
1715 "w",
1716 "h",
1717 "fps",
1718 "horfreq",
1719 "pixclk",
1720 "interlaced",
1721 "overscan",
1722 "secondary",
1723 "C",
1724 "M",
1725 "K",
1726 "J",
1727 nullptr
1730 opt = getsubopt(subopt_str, (char * const *)subopt_list, &opt_str);
1732 if (opt == -1) {
1733 fprintf(stderr, "Invalid suboptions specified.\n");
1734 usage();
1735 std::exit(EXIT_FAILURE);
1737 if (opt_str == nullptr && opt != GTF_INTERLACED && opt != GTF_OVERSCAN &&
1738 opt != GTF_SECONDARY) {
1739 fprintf(stderr, "No value given to suboption <%s>.\n",
1740 subopt_list[opt]);
1741 usage();
1742 std::exit(EXIT_FAILURE);
1745 if (opt == GTF_C2 || opt == GTF_J2)
1746 *value = round(2.0 * strtod(opt_str, nullptr));
1747 else if (opt_str)
1748 *value = strtod(opt_str, nullptr);
1749 return opt;
1752 static void parse_gtf(char *optarg, gtf_parsed_data &data)
1754 memset(&data, 0, sizeof(data));
1755 data.params_from_edid = true;
1756 data.C = 40;
1757 data.M = 600;
1758 data.K = 128;
1759 data.J = 20;
1761 while (*optarg != '\0') {
1762 int opt;
1763 double opt_val;
1765 opt = parse_gtf_subopt(&optarg, &opt_val);
1767 switch (opt) {
1768 case GTF_WIDTH:
1769 data.w = round(opt_val);
1770 break;
1771 case GTF_HEIGHT:
1772 data.h = round(opt_val);
1773 break;
1774 case GTF_FPS:
1775 data.freq = opt_val;
1776 data.ip_parm = gtf_ip_vert_freq;
1777 break;
1778 case GTF_HORFREQ:
1779 data.freq = opt_val;
1780 data.ip_parm = gtf_ip_hor_freq;
1781 break;
1782 case GTF_PIXCLK:
1783 data.freq = opt_val;
1784 data.ip_parm = gtf_ip_clk_freq;
1785 break;
1786 case GTF_INTERLACED:
1787 data.interlaced = true;
1788 break;
1789 case GTF_OVERSCAN:
1790 data.overscan = true;
1791 break;
1792 case GTF_SECONDARY:
1793 data.secondary = true;
1794 break;
1795 case GTF_C2:
1796 data.C = opt_val / 2.0;
1797 data.params_from_edid = false;
1798 break;
1799 case GTF_M:
1800 data.M = round(opt_val);
1801 data.params_from_edid = false;
1802 break;
1803 case GTF_K:
1804 data.K = round(opt_val);
1805 data.params_from_edid = false;
1806 break;
1807 case GTF_J2:
1808 data.J = opt_val / 2.0;
1809 data.params_from_edid = false;
1810 break;
1811 default:
1812 break;
1816 if (!data.w || !data.h) {
1817 fprintf(stderr, "Missing width and/or height.\n");
1818 usage();
1819 std::exit(EXIT_FAILURE);
1821 if (!data.freq) {
1822 fprintf(stderr, "One of fps, horfreq or pixclk must be given.\n");
1823 usage();
1824 std::exit(EXIT_FAILURE);
1826 if (!data.secondary)
1827 data.params_from_edid = false;
1828 if (data.interlaced && data.ip_parm == gtf_ip_vert_freq)
1829 data.freq /= 2;
1832 static void show_gtf(gtf_parsed_data &data)
1834 timings t;
1836 t = state.calc_gtf_mode(data.w, data.h, data.freq, data.interlaced,
1837 data.ip_parm, data.overscan, data.secondary,
1838 data.C, data.M, data.K, data.J);
1839 calc_ratio(&t);
1840 state.print_timings("", &t, "GTF", "", true, false);
1843 enum ovt_opts {
1844 OVT_RID,
1845 OVT_WIDTH,
1846 OVT_HEIGHT,
1847 OVT_FPS,
1850 static int parse_ovt_subopt(char **subopt_str, unsigned *value)
1852 int opt;
1853 char *opt_str;
1855 static const char * const subopt_list[] = {
1856 "rid",
1857 "w",
1858 "h",
1859 "fps",
1860 nullptr
1863 opt = getsubopt(subopt_str, (char* const*) subopt_list, &opt_str);
1865 if (opt == -1) {
1866 fprintf(stderr, "Invalid suboptions specified.\n");
1867 usage();
1868 std::exit(EXIT_FAILURE);
1870 if (opt_str == nullptr) {
1871 fprintf(stderr, "No value given to suboption <%s>.\n",
1872 subopt_list[opt]);
1873 usage();
1874 std::exit(EXIT_FAILURE);
1877 if (opt_str)
1878 *value = strtoul(opt_str, NULL, 0);
1879 return opt;
1882 static void parse_ovt(char *optarg)
1884 unsigned rid = 0;
1885 unsigned w = 0, h = 0;
1886 unsigned fps = 0;
1888 while (*optarg != '\0') {
1889 int opt;
1890 unsigned opt_val;
1892 opt = parse_ovt_subopt(&optarg, &opt_val);
1894 switch (opt) {
1895 case OVT_RID:
1896 rid = opt_val;
1897 break;
1898 case OVT_WIDTH:
1899 w = opt_val;
1900 break;
1901 case OVT_HEIGHT:
1902 h = opt_val;
1903 break;
1904 case OVT_FPS:
1905 fps = opt_val;
1906 break;
1907 default:
1908 break;
1912 if ((!rid && (!w || !h)) || !fps) {
1913 fprintf(stderr, "Missing rid, width, height and/or fps.\n");
1914 usage();
1915 std::exit(EXIT_FAILURE);
1917 unsigned hratio = 0, vratio = 0;
1918 if (rid) {
1919 const cta_rid *r = find_rid(rid);
1921 if (r) {
1922 w = r->hact;
1923 h = r->vact;
1924 hratio = r->hratio;
1925 vratio = r->vratio;
1928 timings t = state.calc_ovt_mode(w, h, hratio, vratio, fps);
1929 state.print_timings("", &t, "OVT", "", true, false);
1932 int main(int argc, char **argv)
1934 char short_options[26 * 2 * 2 + 1];
1935 enum output_format out_fmt = OUT_FMT_DEFAULT;
1936 gtf_parsed_data gtf_data;
1937 unsigned list_rid = 0;
1938 int ret;
1940 while (1) {
1941 int option_index = 0;
1942 unsigned idx = 0;
1943 unsigned i, val;
1944 const timings *t;
1945 char buf[16];
1947 for (i = 0; long_options[i].name; i++) {
1948 if (!isalpha(long_options[i].val))
1949 continue;
1950 short_options[idx++] = long_options[i].val;
1951 if (long_options[i].has_arg == required_argument)
1952 short_options[idx++] = ':';
1954 short_options[idx] = 0;
1955 int ch = getopt_long(argc, argv, short_options,
1956 long_options, &option_index);
1957 if (ch == -1)
1958 break;
1960 options[ch] = 1;
1961 switch (ch) {
1962 case OptHelp:
1963 usage();
1964 return -1;
1965 case OptOutputFormat:
1966 if (!strcmp(optarg, "hex")) {
1967 out_fmt = OUT_FMT_HEX;
1968 } else if (!strcmp(optarg, "raw")) {
1969 out_fmt = OUT_FMT_RAW;
1970 } else if (!strcmp(optarg, "carray")) {
1971 out_fmt = OUT_FMT_CARRAY;
1972 } else if (!strcmp(optarg, "xml")) {
1973 out_fmt = OUT_FMT_XML;
1974 } else {
1975 usage();
1976 exit(1);
1978 break;
1979 case OptDiag:
1980 state.diagonal = strtod(optarg, NULL);
1981 break;
1982 case OptSTD: {
1983 unsigned char byte1, byte2 = 0;
1984 char *endptr;
1986 byte1 = strtoul(optarg, &endptr, 0);
1987 if (*endptr == ',')
1988 byte2 = strtoul(endptr + 1, NULL, 0);
1989 state.print_standard_timing("", byte1, byte2, false, true);
1990 break;
1992 case OptDMT:
1993 val = strtoul(optarg, NULL, 0);
1994 t = find_dmt_id(val);
1995 if (t) {
1996 sprintf(buf, "DMT 0x%02x", val);
1997 state.print_timings("", t, buf, "", true, false);
1998 } else {
1999 fprintf(stderr, "Unknown DMT code 0x%02x.\n", val);
2001 break;
2002 case OptVIC:
2003 val = strtoul(optarg, NULL, 0);
2004 t = find_vic_id(val);
2005 if (t) {
2006 sprintf(buf, "VIC %3u", val);
2007 state.print_timings("", t, buf, "", true, false);
2008 } else {
2009 fprintf(stderr, "Unknown VIC code %u.\n", val);
2011 break;
2012 case OptHDMIVIC:
2013 val = strtoul(optarg, NULL, 0);
2014 t = find_hdmi_vic_id(val);
2015 if (t) {
2016 sprintf(buf, "HDMI VIC %u", val);
2017 state.print_timings("", t, buf, "", true, false);
2018 } else {
2019 fprintf(stderr, "Unknown HDMI VIC code %u.\n", val);
2021 break;
2022 case OptCVT:
2023 parse_cvt(optarg);
2024 break;
2025 case OptGTF:
2026 parse_gtf(optarg, gtf_data);
2027 break;
2028 case OptOVT:
2029 parse_ovt(optarg);
2030 break;
2031 case OptListRIDTimings:
2032 list_rid = strtoul(optarg, NULL, 0);
2033 break;
2034 case ':':
2035 fprintf(stderr, "Option '%s' requires a value.\n",
2036 argv[optind]);
2037 usage();
2038 return -1;
2039 case '?':
2040 fprintf(stderr, "Unknown argument '%s'.\n",
2041 argv[optind]);
2042 usage();
2043 return -1;
2046 if (optind == argc && options[OptVersion]) {
2047 if (strlen(STRING(SHA)))
2048 printf("edid-decode SHA: %s %s\n", STRING(SHA), STRING(DATE));
2049 else
2050 printf("edid-decode SHA: not available\n");
2051 return 0;
2054 if (options[OptListEstTimings])
2055 state.list_established_timings();
2056 if (options[OptListDMTs])
2057 state.list_dmts();
2058 if (options[OptListVICs])
2059 state.cta_list_vics();
2060 if (options[OptListHDMIVICs])
2061 state.cta_list_hdmi_vics();
2062 if (options[OptListRIDs])
2063 state.cta_list_rids();
2064 if (options[OptListRIDTimings])
2065 state.cta_list_rid_timings(list_rid);
2067 if (options[OptListEstTimings] || options[OptListDMTs] ||
2068 options[OptListVICs] || options[OptListHDMIVICs] ||
2069 options[OptListRIDs] || options[OptListRIDTimings])
2070 return 0;
2072 if (options[OptCVT] || options[OptDMT] || options[OptVIC] ||
2073 options[OptHDMIVIC] || options[OptSTD] || options[OptOVT])
2074 return 0;
2076 if (options[OptGTF] && (!gtf_data.params_from_edid || optind == argc)) {
2077 show_gtf(gtf_data);
2078 return 0;
2081 if (optind == argc)
2082 ret = edid_from_file("-", stdout);
2083 else
2084 ret = edid_from_file(argv[optind], argv[optind + 1] ? stderr : stdout);
2086 if (ret && options[OptPhysicalAddress]) {
2087 printf("f.f.f.f\n");
2088 return 0;
2090 if (optind < argc - 1)
2091 return ret ? ret : edid_to_file(argv[optind + 1], out_fmt);
2093 if (options[OptGTF]) {
2094 timings t;
2096 // Find the Secondary Curve
2097 state.preparse_detailed_block(edid + 0x36);
2098 state.preparse_detailed_block(edid + 0x48);
2099 state.preparse_detailed_block(edid + 0x5a);
2100 state.preparse_detailed_block(edid + 0x6c);
2102 t = state.calc_gtf_mode(gtf_data.w, gtf_data.h, gtf_data.freq,
2103 gtf_data.interlaced, gtf_data.ip_parm,
2104 gtf_data.overscan);
2105 unsigned hbl = t.hfp + t.hsync + t.hbp;
2106 unsigned htotal = t.hact + hbl;
2107 double hor_freq_khz = htotal ? (double)t.pixclk_khz / htotal : 0;
2109 if (state.base.supports_sec_gtf &&
2110 hor_freq_khz >= state.base.sec_gtf_start_freq) {
2111 t = state.calc_gtf_mode(gtf_data.w, gtf_data.h, gtf_data.freq,
2112 gtf_data.interlaced, gtf_data.ip_parm,
2113 gtf_data.overscan, true,
2114 state.base.C, state.base.M,
2115 state.base.K, state.base.J);
2117 calc_ratio(&t);
2118 if (t.hfp <= 0)
2119 state.print_timings("", &t, "GTF", "INVALID: Hfront <= 0", true, false);
2120 else
2121 state.print_timings("", &t, "GTF", "", true, false);
2122 return 0;
2125 return ret ? ret : state.parse_edid();
2128 #else
2131 * The surrounding JavaScript implementation will call this function
2132 * each time it wants to decode an EDID. So this should reset all the
2133 * state and start over.
2135 extern "C" int parse_edid(const char *input)
2137 for (unsigned i = 0; i < EDID_MAX_BLOCKS + 1; i++) {
2138 s_msgs[i][0].clear();
2139 s_msgs[i][1].clear();
2141 options[OptCheck] = 1;
2142 options[OptPreferredTimings] = 1;
2143 options[OptNativeResolution] = 1;
2144 state = edid_state();
2145 int ret = edid_from_file(input, stderr);
2146 return ret ? ret : state.parse_edid();
2149 #endif