edid-decode: minor modifications to README and emscripten path
[edid-decode.git] / edid-decode.cpp
blob48e5c4d51eb832647558771a523d8e488fd4d04b
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 OptReplaceUniqueIDs,
61 OptVersion,
62 OptDiag,
63 OptSTD,
64 OptDMT,
65 OptVIC,
66 OptHDMIVIC,
67 OptCVT,
68 OptGTF,
69 OptOVT,
70 OptListEstTimings,
71 OptListDMTs,
72 OptListVICs,
73 OptListHDMIVICs,
74 OptListRIDTimings,
75 OptListRIDs,
76 OptLast = 256
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 },
116 { 0, 0, 0, 0 }
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"
126 "\nOptions:\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"
133 " xml: XML data\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, dates, 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");
199 #endif
201 static std::string s_msgs[EDID_MAX_BLOCKS + 1][2];
203 void msg(bool is_warn, const char *fmt, ...)
205 char buf[1024] = "";
206 va_list ap;
208 va_start(ap, fmt);
209 vsprintf(buf, fmt, ap);
210 va_end(ap);
212 if (is_warn)
213 state.warnings++;
214 else
215 state.failures++;
216 if (state.data_block.empty())
217 s_msgs[state.block_nr][is_warn] += std::string(" ") + buf;
218 else
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())
230 continue;
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())
236 return;
237 printf("EDID:\n%s",
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;
245 unsigned i;
247 for (i = 0; i < len - 1; i++)
248 sum += x[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;
256 unsigned i;
258 for (i = 0; i < len - 1; i++)
259 sum += x[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",
265 check, -sum & 0xff);
267 if (unused_bytes)
268 printf(" Unused space in Extension Block: %u byte%s",
269 unused_bytes, unused_bytes > 1 ? "s" : "");
270 printf("\n");
273 unsigned gcd(unsigned a, unsigned b)
275 while (b) {
276 unsigned t = b;
278 b = a % b;
279 a = t;
281 return a;
284 void calc_ratio(struct timings *t)
286 unsigned d = gcd(t->hact, t->vact);
288 if (d == 0) {
289 t->hratio = t->vratio = 0;
290 return;
292 t->hratio = t->hact / d;
293 t->vratio = t->vact / d;
295 if (t->hratio == 8 && t->vratio == 5) {
296 t->hratio = 16;
297 t->vratio = 10;
301 std::string edid_state::dtd_type(unsigned cnt)
303 unsigned len = std::to_string(cta.preparsed_total_dtds).length();
304 char buf[16];
305 sprintf(buf, "DTD %*u", len, cnt);
306 return buf;
309 bool match_timings(const timings &t1, const timings &t2)
311 if (t1.hact != t2.hact ||
312 t1.vact != t2.vact ||
313 t1.rb != t2.rb ||
314 t1.interlaced != t2.interlaced ||
315 t1.hfp != t2.hfp ||
316 t1.hbp != t2.hbp ||
317 t1.hsync != t2.hsync ||
318 t1.pos_pol_hsync != t2.pos_pol_hsync ||
319 t1.hratio != t2.hratio ||
320 t1.vfp != t2.vfp ||
321 t1.vbp != t2.vbp ||
322 t1.vsync != t2.vsync ||
323 t1.pos_pol_vsync != t2.pos_pol_vsync ||
324 t1.vratio != t2.vratio ||
325 t1.pixclk_khz != t2.pixclk_khz)
326 return false;
327 return true;
330 static void or_str(std::string &s, const std::string &flag, unsigned &num_flags)
332 if (!num_flags)
333 s = flag;
334 else if (num_flags % 2 == 0)
335 s = s + " | \\\n\t\t" + flag;
336 else
337 s = s + " | " + flag;
338 num_flags++;
342 * Return true if the timings are a close, but not identical,
343 * match. The only differences allowed are polarities and
344 * porches and syncs, provided the total blanking remains the
345 * same.
347 bool timings_close_match(const timings &t1, const timings &t2)
349 // We don't want to deal with borders, you're on your own
350 // if you are using those.
351 if (t1.hborder || t1.vborder ||
352 t2.hborder || t2.vborder)
353 return false;
354 if (t1.hact != t2.hact || t1.vact != t2.vact ||
355 t1.interlaced != t2.interlaced ||
356 t1.pixclk_khz != t2.pixclk_khz ||
357 t1.hfp + t1.hsync + t1.hbp != t2.hfp + t2.hsync + t2.hbp ||
358 t1.vfp + t1.vsync + t1.vbp != t2.vfp + t2.vsync + t2.vbp)
359 return false;
360 if (t1.hfp == t2.hfp &&
361 t1.hsync == t2.hsync &&
362 t1.hbp == t2.hbp &&
363 t1.pos_pol_hsync == t2.pos_pol_hsync &&
364 t1.vfp == t2.vfp &&
365 t1.vsync == t2.vsync &&
366 t1.vbp == t2.vbp &&
367 t1.pos_pol_vsync == t2.pos_pol_vsync)
368 return false;
369 return true;
372 static void print_modeline(unsigned indent, const struct timings *t, double refresh)
374 unsigned offset = (!t->even_vtotal && t->interlaced) ? 1 : 0;
375 unsigned hfp = t->hborder + t->hfp;
376 unsigned hbp = t->hborder + t->hbp;
377 unsigned vfp = t->vborder + t->vfp;
378 unsigned vbp = t->vborder + t->vbp;
380 printf("%*sModeline \"%ux%u_%.2f%s\" %.3f %u %u %u %u %u %u %u %u %cHSync",
381 indent, "",
382 t->hact, t->vact, refresh,
383 t->interlaced ? "i" : "", t->pixclk_khz / 1000.0,
384 t->hact, t->hact + hfp, t->hact + hfp + t->hsync,
385 t->hact + hfp + t->hsync + hbp,
386 t->vact, t->vact + vfp, t->vact + vfp + t->vsync,
387 t->vact + vfp + t->vsync + vbp + offset,
388 t->pos_pol_hsync ? '+' : '-');
389 if (!t->no_pol_vsync)
390 printf(" %cVSync", t->pos_pol_vsync ? '+' : '-');
391 if (t->interlaced)
392 printf(" Interlace");
393 printf("\n");
396 static void print_fbmode(unsigned indent, const struct timings *t,
397 double refresh, double hor_freq_khz)
399 printf("%*smode \"%ux%u-%u%s\"\n",
400 indent, "",
401 t->hact, t->vact,
402 (unsigned)(0.5 + (t->interlaced ? refresh / 2.0 : refresh)),
403 t->interlaced ? "-lace" : "");
404 printf("%*s# D: %.2f MHz, H: %.3f kHz, V: %.2f Hz\n",
405 indent + 8, "",
406 t->pixclk_khz / 1000.0, hor_freq_khz, refresh);
407 printf("%*sgeometry %u %u %u %u 32\n",
408 indent + 8, "",
409 t->hact, t->vact, t->hact, t->vact);
410 unsigned mult = t->interlaced ? 2 : 1;
411 unsigned offset = !t->even_vtotal && t->interlaced;
412 unsigned hfp = t->hborder + t->hfp;
413 unsigned hbp = t->hborder + t->hbp;
414 unsigned vfp = t->vborder + t->vfp;
415 unsigned vbp = t->vborder + t->vbp;
416 printf("%*stimings %llu %d %d %d %u %u %u\n",
417 indent + 8, "",
418 (unsigned long long)(1000000000.0 / (double)(t->pixclk_khz) + 0.5),
419 hbp, hfp, mult * vbp, mult * vfp + offset, t->hsync, mult * t->vsync);
420 if (t->interlaced)
421 printf("%*slaced true\n", indent + 8, "");
422 if (t->pos_pol_hsync)
423 printf("%*shsync high\n", indent + 8, "");
424 if (t->pos_pol_vsync)
425 printf("%*svsync high\n", indent + 8, "");
426 printf("%*sendmode\n", indent, "");
429 static void print_v4l2_timing(const struct timings *t,
430 double refresh, const char *type)
432 printf("\t#define V4L2_DV_BT_%uX%u%c%u_%02u { \\\n",
433 t->hact, t->vact, t->interlaced ? 'I' : 'P',
434 (unsigned)refresh, (unsigned)(0.5 + 100.0 * (refresh - (unsigned)refresh)));
435 printf("\t\t.type = V4L2_DV_BT_656_1120, \\\n");
436 printf("\t\tV4L2_INIT_BT_TIMINGS(%u, %u, %u, ",
437 t->hact, t->vact, t->interlaced);
438 if (!t->pos_pol_hsync && !t->pos_pol_vsync)
439 printf("0, \\\n");
440 else if (t->pos_pol_hsync && t->pos_pol_vsync)
441 printf("\\\n\t\t\tV4L2_DV_HSYNC_POS_POL | V4L2_DV_VSYNC_POS_POL, \\\n");
442 else if (t->pos_pol_hsync)
443 printf("V4L2_DV_HSYNC_POS_POL, \\\n");
444 else
445 printf("V4L2_DV_VSYNC_POS_POL, \\\n");
446 unsigned hfp = t->hborder + t->hfp;
447 unsigned hbp = t->hborder + t->hbp;
448 unsigned vfp = t->vborder + t->vfp;
449 unsigned vbp = t->vborder + t->vbp;
450 printf("\t\t\t%lluULL, %d, %u, %d, %u, %u, %d, %u, %u, %d, \\\n",
451 t->pixclk_khz * 1000ULL, hfp, t->hsync, hbp,
452 vfp, t->vsync, vbp,
453 t->interlaced ? vfp : 0,
454 t->interlaced ? t->vsync : 0,
455 t->interlaced ? vbp + !t->even_vtotal : 0);
457 std::string flags;
458 unsigned num_flags = 0;
459 unsigned vic = 0;
460 unsigned hdmi_vic = 0;
461 const char *std = "0";
463 if (t->interlaced && !t->even_vtotal)
464 or_str(flags, "V4L2_DV_FL_HALF_LINE", num_flags);
465 if (!memcmp(type, "VIC", 3)) {
466 or_str(flags, "V4L2_DV_FL_HAS_CEA861_VIC", num_flags);
467 or_str(flags, "V4L2_DV_FL_IS_CE_VIDEO", num_flags);
468 vic = strtoul(type + 4, 0, 0);
470 if (!memcmp(type, "HDMI VIC", 8)) {
471 or_str(flags, "V4L2_DV_FL_HAS_HDMI_VIC", num_flags);
472 or_str(flags, "V4L2_DV_FL_IS_CE_VIDEO", num_flags);
473 hdmi_vic = strtoul(type + 9, 0, 0);
474 vic = hdmi_vic_to_vic(hdmi_vic);
475 if (vic)
476 or_str(flags, "V4L2_DV_FL_HAS_CEA861_VIC", num_flags);
478 if (vic && (fmod(refresh, 6)) == 0.0)
479 or_str(flags, "V4L2_DV_FL_CAN_REDUCE_FPS", num_flags);
480 if (t->rb)
481 or_str(flags, "V4L2_DV_FL_REDUCED_BLANKING", num_flags);
482 if (t->hratio && t->vratio)
483 or_str(flags, "V4L2_DV_FL_HAS_PICTURE_ASPECT", num_flags);
485 if (!memcmp(type, "VIC", 3) || !memcmp(type, "HDMI VIC", 8))
486 std = "V4L2_DV_BT_STD_CEA861";
487 else if (!memcmp(type, "DMT", 3))
488 std = "V4L2_DV_BT_STD_DMT";
489 else if (!memcmp(type, "CVT", 3))
490 std = "V4L2_DV_BT_STD_CVT";
491 else if (!memcmp(type, "GTF", 3))
492 std = "V4L2_DV_BT_STD_GTF";
493 printf("\t\t\t%s, \\\n", std);
494 printf("\t\t\t%s, \\\n", flags.empty() ? "0" : flags.c_str());
495 printf("\t\t\t{ %u, %u }, %u, %u) \\\n",
496 t->hratio, t->vratio, vic, hdmi_vic);
497 printf("\t}\n");
500 static void print_detailed_timing(unsigned indent, const struct timings *t)
502 printf("%*sHfront %4d Hsync %3u Hback %4d Hpol %s",
503 indent, "",
504 t->hfp, t->hsync, t->hbp, t->pos_pol_hsync ? "P" : "N");
505 if (t->hborder)
506 printf(" Hborder %u", t->hborder);
507 printf("\n");
509 printf("%*sVfront %4u Vsync %3u Vback %4d",
510 indent, "", t->vfp, t->vsync, t->vbp);
511 if (!t->no_pol_vsync)
512 printf(" Vpol %s", t->pos_pol_vsync ? "P" : "N");
513 if (t->vborder)
514 printf(" Vborder %u", t->vborder);
515 if (t->even_vtotal) {
516 printf(" Both Fields");
517 } else if (t->interlaced) {
518 printf(" Vfront +0.5 Odd Field\n");
519 printf("%*sVfront %4d Vsync %3u Vback %4d",
520 indent, "", t->vfp, t->vsync, t->vbp);
521 if (!t->no_pol_vsync)
522 printf(" Vpol %s", t->pos_pol_vsync ? "P" : "N");
523 if (t->vborder)
524 printf(" Vborder %u", t->vborder);
525 printf(" Vback +0.5 Even Field");
527 printf("\n");
530 bool edid_state::print_timings(const char *prefix, const struct timings *t,
531 const char *type, const char *flags,
532 bool detailed, bool do_checks)
534 if (!t) {
535 // Should not happen
536 if (do_checks)
537 fail("Unknown video timings.\n");
538 return false;
541 if (detailed && options[OptShortTimings])
542 detailed = false;
543 if (options[OptLongTimings])
544 detailed = true;
546 unsigned vact = t->vact;
547 unsigned hbl = t->hfp + t->hsync + t->hbp + 2 * t->hborder;
548 unsigned vbl = t->vfp + t->vsync + t->vbp + 2 * t->vborder;
549 unsigned htotal = t->hact + hbl;
550 double hor_freq_khz = htotal ? (double)t->pixclk_khz / htotal : 0;
552 if (t->interlaced)
553 vact /= 2;
555 double out_hor_freq_khz = hor_freq_khz;
556 if (t->ycbcr420)
557 hor_freq_khz /= 2;
559 double vtotal = vact + vbl;
561 bool ok = true;
563 if (!t->hact || !hbl || !t->hfp || !t->hsync ||
564 !vact || !vbl || (!t->vfp && !t->interlaced && !t->even_vtotal) || !t->vsync) {
565 if (do_checks)
566 fail("0 values in the video timing:\n"
567 " Horizontal Active/Blanking %u/%u\n"
568 " Horizontal Frontporch/Sync Width %u/%u\n"
569 " Vertical Active/Blanking %u/%u\n"
570 " Vertical Frontporch/Sync Width %u/%u\n",
571 t->hact, hbl, t->hfp, t->hsync, vact, vbl, t->vfp, t->vsync);
572 ok = false;
575 if (t->even_vtotal)
576 vtotal = vact + t->vfp + t->vsync + t->vbp;
577 else if (t->interlaced)
578 vtotal = vact + t->vfp + t->vsync + t->vbp + 0.5;
580 double refresh = t->pixclk_khz * 1000.0 / (htotal * vtotal);
581 double pixclk = t->pixclk_khz * 1000.0;
582 if (options[OptNTSC] && fmod(refresh, 6.0) == 0) {
583 const double ntsc_fact = 1000.0 / 1001.0;
584 pixclk *= ntsc_fact;
585 refresh *= ntsc_fact;
586 out_hor_freq_khz *= ntsc_fact;
589 std::string s;
590 unsigned rb = t->rb & ~RB_ALT;
591 if (rb) {
592 bool alt = t->rb & RB_ALT;
593 s = "RB";
594 if (rb == RB_CVT_V2)
595 s += std::string("v2") + (alt ? ",video-optimized" : "");
596 else if (rb == RB_CVT_V3)
597 s += std::string("v3") + (alt ? ",h-blank-160" : "");
599 add_str(s, flags);
600 if (t->hsize_mm || t->vsize_mm)
601 add_str(s, std::to_string(t->hsize_mm) + " mm x " + std::to_string(t->vsize_mm) + " mm");
602 if (t->hsize_mm > dtd_max_hsize_mm)
603 dtd_max_hsize_mm = t->hsize_mm;
604 if (t->vsize_mm > dtd_max_vsize_mm)
605 dtd_max_vsize_mm = t->vsize_mm;
606 if (!s.empty())
607 s = " (" + s + ")";
608 unsigned pixclk_khz = t->pixclk_khz / (t->ycbcr420 ? 2 : 1);
610 char buf[10];
612 sprintf(buf, "%u%s", t->vact, t->interlaced ? "i" : "");
613 printf("%s%s: %5ux%-5s %10.6f Hz %3u:%-3u %8.3f kHz %13.6f MHz%s\n",
614 prefix, type,
615 t->hact, buf,
616 refresh,
617 t->hratio, t->vratio,
618 out_hor_freq_khz,
619 pixclk / 1000000.0,
620 s.c_str());
622 unsigned len = strlen(prefix) + 2;
624 if (!t->ycbcr420 && detailed && options[OptXModeLineTimings])
625 print_modeline(len, t, refresh);
626 else if (!t->ycbcr420 && detailed && options[OptFBModeTimings])
627 print_fbmode(len, t, refresh, hor_freq_khz);
628 else if (!t->ycbcr420 && detailed && options[OptV4L2Timings])
629 print_v4l2_timing(t, refresh, type);
630 else if (detailed)
631 print_detailed_timing(len + strlen(type) + 6, t);
633 if (!do_checks)
634 return ok;
636 if (!memcmp(type, "DTD", 3)) {
637 unsigned vic, dmt;
638 const timings *vic_t = cta_close_match_to_vic(*t, vic);
640 // We report this even if there is no CTA block since it
641 // is still likely that the actual VIC timings were intended.
642 if (vic_t)
643 warn("DTD is similar but not identical to VIC %u.\n", vic);
645 if (cta_matches_vic(*t, vic) && has_cta &&
646 !cta.preparsed_has_vic[0][vic]) {
647 warn("DTD is identical to VIC %u, which is not present in the CTA Ext Block.\n", vic);
649 if (cta.preparsed_max_vic_pixclk_khz && t->pixclk_khz > 340000 &&
650 t->pixclk_khz > cta.preparsed_max_vic_pixclk_khz)
651 cta.warn_about_hdmi_2x_dtd = true;
654 const timings *dmt_t = close_match_to_dmt(*t, dmt);
655 if (!vic_t && dmt_t)
656 warn("DTD is similar but not identical to DMT 0x%02x.\n", dmt);
659 if (refresh) {
660 min_vert_freq_hz = min(min_vert_freq_hz, refresh);
661 max_vert_freq_hz = max(max_vert_freq_hz, refresh);
663 if (hor_freq_khz) {
664 min_hor_freq_hz = min(min_hor_freq_hz, hor_freq_khz * 1000.0);
665 max_hor_freq_hz = max(max_hor_freq_hz, hor_freq_khz * 1000.0);
666 max_pixclk_khz = max(max_pixclk_khz, pixclk_khz);
667 if (t->pos_pol_hsync && !t->pos_pol_vsync && t->vsync == 3)
668 base.max_pos_neg_hor_freq_khz = hor_freq_khz;
671 if (t->ycbcr420 && t->pixclk_khz < 590000)
672 warn_once("Some YCbCr 4:2:0 timings are invalid for HDMI 2.1 (which requires an RGB timings pixel rate >= 590 MHz).\n");
673 if (t->hfp <= 0)
674 fail("0 or negative horizontal front porch.\n");
675 if (t->hbp <= 0)
676 fail("0 or negative horizontal back porch.\n");
677 if (t->vbp <= 0)
678 fail("0 or negative vertical back porch.\n");
679 if (!base.max_display_width_mm && !base.max_display_height_mm) {
680 /* this is valid */
681 } else if (!t->hsize_mm && !t->vsize_mm) {
682 /* this is valid */
683 } else if (t->hsize_mm > base.max_display_width_mm + 9 ||
684 t->vsize_mm > base.max_display_height_mm + 9) {
685 fail("Mismatch of image size %ux%u mm vs display size %ux%u mm.\n",
686 t->hsize_mm, t->vsize_mm, base.max_display_width_mm, base.max_display_height_mm);
687 } else if (t->hsize_mm < base.max_display_width_mm - 9 &&
688 t->vsize_mm < base.max_display_height_mm - 9) {
689 fail("Mismatch of image size %ux%u mm vs display size %ux%u mm.\n",
690 t->hsize_mm, t->vsize_mm, base.max_display_width_mm, base.max_display_height_mm);
692 return ok;
695 std::string containerid2s(const unsigned char *x)
697 char buf[40];
699 sprintf(buf, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
700 x[0], x[1], x[2], x[3],
701 x[4], x[5],
702 x[6], x[7],
703 x[8], x[9],
704 x[10], x[11], x[12], x[13], x[14], x[15]);
705 return buf;
708 std::string utohex(unsigned char x)
710 char buf[10];
712 sprintf(buf, "0x%02hhx", x);
713 return buf;
716 const char *oui_name(unsigned oui, unsigned *ouinum)
718 unsigned ouinumscratch;
719 if (!ouinum) ouinum = &ouinumscratch;
720 const char *name;
721 switch (oui) {
722 #define oneoui(c,k,n) case c: *ouinum = kOUI_##k; name = n; break;
723 #include "oui.h"
724 default: *ouinum = 0; name = NULL; break;
726 return name;
729 void edid_state::data_block_oui(std::string block_name, const unsigned char *x,
730 unsigned length, unsigned *ouinum, bool ignorezeros, bool do_ascii, bool big_endian)
732 std::string buf;
733 char ascii[4];
734 unsigned oui;
735 const char *ouiname = NULL;
736 bool matched_reverse = false;
737 bool matched_ascii = false;
738 bool valid_ascii = false;
740 if (big_endian)
741 oui = ((length > 0 ? x[0] : 0) << 16) + ((length > 1 ? x[1] : 0) << 8) + (length > 2 ? x[2] : 0);
742 else
743 oui = ((length > 2 ? x[2] : 0) << 16) + ((length > 1 ? x[1] : 0) << 8) + (length > 0 ? x[0] : 0);
745 buf = ouitohex(oui);
746 if (length < 3) {
747 sprintf(ascii, "?"); // some characters are null
748 if (ouinum) *ouinum = 0; // doesn't match a known OUI
749 } else {
750 valid_ascii = (x[0] >= 'A' && x[1] >= 'A' && x[2] >= 'A' && x[0] <= 'Z' && x[1] <= 'Z' && x[2] <= 'Z');
751 sprintf(ascii, "%c%c%c", x[0], x[1], x[2]);
753 ouiname = oui_name(oui, ouinum);
754 if (!ouiname) {
755 big_endian = !big_endian;
756 unsigned reversedoui = ((oui & 0xff) << 16) + (oui & 0x00ff00) + (oui >> 16);
757 ouiname = oui_name(reversedoui, ouinum);
758 if (ouiname) {
759 oui = reversedoui;
760 buf = ouitohex(oui);
761 matched_reverse = true;
762 } else if (do_ascii && valid_ascii) {
763 unsigned asciioui = (x[0] << 24) + (x[1] << 16) + (x[2] << 8);
764 ouiname = oui_name(asciioui, ouinum);
765 if (ouiname) {
766 matched_ascii = true;
772 std::string name;
773 if (ouiname) {
774 if (matched_ascii)
775 name = block_name + " (" + ouiname + ")" + ", PNP ID '" + ascii + "'";
776 else
777 name = block_name + " (" + ouiname + ")" + ", OUI " + buf;
778 } else if (do_ascii && valid_ascii) {
779 name = block_name + ", PNP ID '" + ascii + "'";
780 } else {
781 name = block_name + ", OUI " + buf;
783 // assign string to data_block before outputting errors
784 data_block = name;
786 if (oui || !ignorezeros) {
787 printf(" %s:\n", data_block.c_str());
788 if (length < 3)
789 fail("Data block length (%d) is not enough to contain an OUI.\n", length);
790 else if (ouiname) {
791 if (do_ascii && !valid_ascii)
792 warn("Expected PNP ID but found OUI.\n");
793 if (matched_reverse)
794 fail("Endian-ness (%s) of OUI is different than expected (%s).\n", big_endian ? "be" : "le", big_endian ? "le" : "be");
796 else {
797 if (valid_ascii)
798 warn("Unknown OUI %s (possible PNP %s).\n", buf.c_str(), ascii);
799 else
800 warn("Unknown OUI %s.\n", buf.c_str());
805 std::string ouitohex(unsigned oui)
807 char buf[32];
809 sprintf(buf, "%02X-%02X-%02X", (oui >> 16) & 0xff, (oui >> 8) & 0xff, oui & 0xff);
810 return buf;
813 bool memchk(const unsigned char *x, unsigned len, unsigned char v)
815 for (unsigned i = 0; i < len; i++)
816 if (x[i] != v)
817 return false;
818 return true;
821 void hex_block(const char *prefix, const unsigned char *x,
822 unsigned length, bool show_ascii, unsigned step)
824 unsigned i, j;
826 for (i = 0; i < length; i += step) {
827 unsigned len = min(step, length - i);
829 printf("%s", prefix);
830 for (j = 0; j < len; j++)
831 printf("%s%02x", j ? " " : "", x[i + j]);
833 if (show_ascii) {
834 for (j = len; j < step; j++)
835 printf(" ");
836 printf(" '");
837 for (j = 0; j < len; j++)
838 printf("%c", x[i + j] >= ' ' && x[i + j] <= '~' ? x[i + j] : '.');
839 printf("'");
841 printf("\n");
845 static bool edid_add_byte(const char *s, bool two_digits = true)
847 char buf[3];
849 if (state.edid_size == sizeof(edid))
850 return false;
851 buf[0] = s[0];
852 buf[1] = two_digits ? s[1] : 0;
853 buf[2] = 0;
854 edid[state.edid_size++] = strtoul(buf, NULL, 16);
855 return true;
858 static bool extract_edid_quantumdata(const char *start)
860 /* Parse QuantumData 980 EDID files */
861 do {
862 start = strstr(start, ">");
863 if (!start)
864 return false;
865 start++;
866 for (unsigned i = 0; start[i] && start[i + 1] && i < 256; i += 2)
867 if (!edid_add_byte(start + i))
868 return false;
869 start = strstr(start, "<BLOCK");
870 } while (start);
871 return state.edid_size;
874 static const char *ignore_chars = ",:;";
876 static bool extract_edid_hex(const char *s, bool require_two_digits = true)
878 for (; *s; s++) {
879 if (isspace(*s) || strchr(ignore_chars, *s))
880 continue;
882 if (*s == '0' && tolower(s[1]) == 'x') {
883 s++;
884 continue;
887 /* Read one or two hex digits from the log */
888 if (!isxdigit(s[0])) {
889 if (state.edid_size && state.edid_size % 128 == 0)
890 break;
891 return false;
893 if (require_two_digits && !isxdigit(s[1])) {
894 odd_hex_digits = true;
895 return false;
897 if (!edid_add_byte(s, isxdigit(s[1])))
898 return false;
899 if (isxdigit(s[1]))
900 s++;
902 return state.edid_size;
905 static bool extract_edid_xrandr(const char *start)
907 static const char indentation1[] = " ";
908 static const char indentation2[] = "\t\t";
909 /* Used to detect that we've gone past the EDID property */
910 static const char half_indentation1[] = " ";
911 static const char half_indentation2[] = "\t";
912 const char *indentation;
913 const char *s;
915 for (;;) {
916 unsigned j;
918 /* Get the next start of the line of EDID hex, assuming spaces for indentation */
919 s = strstr(start, indentation = indentation1);
920 /* Did we skip the start of another property? */
921 if (s && s > strstr(start, half_indentation1))
922 break;
924 /* If we failed, retry assuming tabs for indentation */
925 if (!s) {
926 s = strstr(start, indentation = indentation2);
927 /* Did we skip the start of another property? */
928 if (s && s > strstr(start, half_indentation2))
929 break;
932 if (!s)
933 break;
935 start = s + strlen(indentation);
937 for (j = 0; j < 16; j++, start += 2) {
938 /* Read a %02x from the log */
939 if (!isxdigit(start[0]) || !isxdigit(start[1])) {
940 if (j)
941 break;
942 return false;
944 if (!edid_add_byte(start))
945 return false;
948 return state.edid_size;
951 static bool extract_edid_xorg(const char *start)
953 bool find_first_num = true;
955 for (; *start; start++) {
956 if (find_first_num) {
957 const char *s;
959 /* skip ahead to the : */
960 s = strstr(start, ": \t");
961 if (!s)
962 s = strstr(start, ": ");
963 if (!s)
964 break;
965 start = s;
966 /* and find the first number */
967 while (!isxdigit(start[1]))
968 start++;
969 find_first_num = false;
970 continue;
971 } else {
972 /* Read a %02x from the log */
973 if (!isxdigit(*start)) {
974 find_first_num = true;
975 continue;
977 if (!edid_add_byte(start))
978 return false;
979 start++;
982 return state.edid_size;
985 static bool extract_edid(int fd, FILE *error)
987 std::vector<char> edid_data;
988 char buf[EDID_PAGE_SIZE];
990 for (;;) {
991 ssize_t i = read(fd, buf, sizeof(buf));
993 if (i < 0)
994 return false;
995 if (i == 0)
996 break;
997 edid_data.insert(edid_data.end(), buf, buf + i);
1000 if (edid_data.empty()) {
1001 state.edid_size = 0;
1002 return false;
1004 // Ensure it is safely terminated by a 0 char
1005 edid_data.push_back('\0');
1007 const char *data = &edid_data[0];
1008 const char *start;
1010 /* Look for edid-decode output */
1011 start = strstr(data, "EDID (hex):");
1012 if (!start)
1013 start = strstr(data, "edid-decode (hex):");
1014 if (start)
1015 return extract_edid_hex(strchr(start, ':'));
1017 /* Look for C-array */
1018 start = strstr(data, "unsigned char edid[] = {");
1019 if (start)
1020 return extract_edid_hex(strchr(start, '{') + 1, false);
1022 /* Look for QuantumData EDID output */
1023 start = strstr(data, "<BLOCK");
1024 if (start)
1025 return extract_edid_quantumdata(start);
1027 /* Look for xrandr --verbose output (lines of 16 hex bytes) */
1028 start = strstr(data, "EDID_DATA:");
1029 if (!start)
1030 start = strstr(data, "EDID:");
1031 if (start)
1032 return extract_edid_xrandr(start);
1034 /* Look for an EDID in an Xorg.0.log file */
1035 start = strstr(data, "EDID (in hex):");
1036 if (start)
1037 start = strstr(start, "(II)");
1038 if (start)
1039 return extract_edid_xorg(start);
1041 unsigned i;
1043 /* Is the EDID provided in hex? */
1044 for (i = 0; i < 32 && (isspace(data[i]) || strchr(ignore_chars, data[i]) ||
1045 tolower(data[i]) == 'x' || isxdigit(data[i])); i++);
1047 if (i == 32)
1048 return extract_edid_hex(data);
1050 // Drop the extra '\0' byte since we now assume binary data
1051 edid_data.pop_back();
1053 /* Assume binary */
1054 if (edid_data.size() > sizeof(edid)) {
1055 fprintf(error, "Binary EDID length %zu is greater than %zu.\n",
1056 edid_data.size(), sizeof(edid));
1057 return false;
1059 memcpy(edid, data, edid_data.size());
1060 state.edid_size = edid_data.size();
1061 return true;
1064 static int edid_from_file(const char *from_file, FILE *error)
1066 #ifdef O_BINARY
1067 // Windows compatibility
1068 int flags = O_RDONLY | O_BINARY;
1069 #else
1070 int flags = O_RDONLY;
1071 #endif
1072 int fd;
1074 if (!strcmp(from_file, "-")) {
1075 from_file = "stdin";
1076 fd = 0;
1077 } else if ((fd = open(from_file, flags)) == -1) {
1078 perror(from_file);
1079 return -1;
1082 odd_hex_digits = false;
1083 if (!extract_edid(fd, error)) {
1084 if (!state.edid_size) {
1085 fprintf(error, "EDID of '%s' was empty.\n", from_file);
1086 return -1;
1088 fprintf(error, "EDID extract of '%s' failed: ", from_file);
1089 if (odd_hex_digits)
1090 fprintf(error, "odd number of hexadecimal digits.\n");
1091 else
1092 fprintf(error, "unknown format.\n");
1093 return -1;
1095 if (state.edid_size % EDID_PAGE_SIZE) {
1096 fprintf(error, "EDID length %u is not a multiple of %u.\n",
1097 state.edid_size, EDID_PAGE_SIZE);
1098 return -1;
1100 state.num_blocks = state.edid_size / EDID_PAGE_SIZE;
1101 if (fd != 0)
1102 close(fd);
1104 if (memcmp(edid, "\x00\xFF\xFF\xFF\xFF\xFF\xFF\x00", 8)) {
1105 fprintf(error, "No EDID header found in '%s'.\n", from_file);
1106 return -1;
1108 return 0;
1111 /* generic extension code */
1113 std::string block_name(unsigned char block)
1115 char buf[10];
1117 switch (block) {
1118 case 0x00: return "Base EDID";
1119 case 0x02: return "CTA-861 Extension Block";
1120 case 0x10: return "Video Timing Extension Block";
1121 case 0x20: return "EDID 2.0 Extension Block";
1122 case 0x40: return "Display Information Extension Block";
1123 case 0x50: return "Localized String Extension Block";
1124 case 0x60: return "Microdisplay Interface Extension Block";
1125 case 0x70: return "DisplayID Extension Block";
1126 case 0xf0: return "Block Map Extension Block";
1127 case 0xff: return "Manufacturer-Specific Extension Block";
1128 default:
1129 sprintf(buf, " 0x%02x", block);
1130 return std::string("Unknown EDID Extension Block") + buf;
1134 void edid_state::parse_block_map(const unsigned char *x)
1136 unsigned last_valid_block_tag = 0;
1137 bool fail_once = false;
1138 unsigned offset = 1;
1139 unsigned i;
1141 if (block_nr == 1)
1142 block_map.saw_block_1 = true;
1143 else if (!block_map.saw_block_1)
1144 fail("No EDID Block Map Extension found in block 1.\n");
1145 else if (block_nr == 128)
1146 block_map.saw_block_128 = true;
1148 if (block_nr > 1)
1149 offset = 128;
1151 for (i = 1; i < 127; i++) {
1152 unsigned block = offset + i;
1154 if (x[i]) {
1155 last_valid_block_tag++;
1156 if (i != last_valid_block_tag && !fail_once) {
1157 fail("Valid block tags are not consecutive.\n");
1158 fail_once = true;
1160 printf(" Block %3u: %s\n", block, block_name(x[i]).c_str());
1161 if (block >= num_blocks) {
1162 if (!fail_once)
1163 fail("Invalid block number %u.\n", block);
1164 fail_once = true;
1165 } else if (x[i] != edid[block * EDID_PAGE_SIZE]) {
1166 fail("Block %u tag mismatch: expected 0x%02x, but got 0x%02x.\n",
1167 block, edid[block * EDID_PAGE_SIZE], x[i]);
1169 } else if (block < num_blocks) {
1170 fail("Block %u tag mismatch: expected 0x%02x, but got 0x00.\n",
1171 block, edid[block * EDID_PAGE_SIZE]);
1176 void edid_state::preparse_extension(unsigned char *x)
1178 switch (x[0]) {
1179 case 0x02:
1180 has_cta = true;
1181 preparse_cta_block(x);
1182 break;
1183 case 0x50:
1184 preparse_ls_ext_block(x);
1185 break;
1186 case 0x70:
1187 has_dispid = true;
1188 preparse_displayid_block(x);
1189 break;
1193 void edid_state::parse_extension(const unsigned char *x)
1195 block = block_name(x[0]);
1196 data_block.clear();
1197 unused_bytes = 0;
1199 printf("\n");
1200 if (block_nr && x[0] == 0)
1201 block = "Unknown EDID Extension Block 0x00";
1202 printf("Block %u, %s:\n", block_nr, block.c_str());
1204 switch (x[0]) {
1205 case 0x02:
1206 parse_cta_block(x);
1207 break;
1208 case 0x10:
1209 parse_vtb_ext_block(x);
1210 break;
1211 case 0x20:
1212 fail("Deprecated extension block for EDID 2.0, do not use.\n");
1213 break;
1214 case 0x40:
1215 parse_di_ext_block(x);
1216 break;
1217 case 0x50:
1218 parse_ls_ext_block(x);
1219 break;
1220 case 0x70:
1221 parse_displayid_block(x);
1222 break;
1223 case 0xf0:
1224 parse_block_map(x);
1225 if (block_nr != 1 && block_nr != 128)
1226 fail("Must be used in block 1 and 128.\n");
1227 break;
1228 default:
1229 hex_block(" ", x, EDID_PAGE_SIZE);
1230 fail("Unknown Extension Block.\n");
1231 break;
1234 data_block.clear();
1235 do_checksum("", x, EDID_PAGE_SIZE, unused_bytes);
1238 void edid_state::print_preferred_timings()
1240 if (base.preferred_timing.is_valid()) {
1241 printf("\n----------------\n");
1242 printf("\nPreferred Video Timing if only Block 0 is parsed:\n");
1243 print_timings(" ", base.preferred_timing, true, false);
1246 if (!cta.preferred_timings.empty()) {
1247 printf("\n----------------\n");
1248 printf("\nPreferred Video Timing%s if Block 0 and CTA-861 Blocks are parsed:\n",
1249 cta.preferred_timings.size() > 1 ? "s" : "");
1250 for (vec_timings_ext::iterator iter = cta.preferred_timings.begin();
1251 iter != cta.preferred_timings.end(); ++iter)
1252 print_timings(" ", *iter, true, false);
1255 if (!cta.preferred_timings_vfpdb.empty()) {
1256 printf("\n----------------\n");
1257 printf("\nPreferred Video Timing%s if Block 0 and CTA-861 Blocks are parsed with VFPDB support:\n",
1258 cta.preferred_timings_vfpdb.size() > 1 ? "s" : "");
1259 for (vec_timings_ext::iterator iter = cta.preferred_timings_vfpdb.begin();
1260 iter != cta.preferred_timings_vfpdb.end(); ++iter)
1261 print_timings(" ", *iter, true, false);
1264 if (!dispid.preferred_timings.empty()) {
1265 printf("\n----------------\n");
1266 printf("\nPreferred Video Timing%s if Block 0 and DisplayID Blocks are parsed:\n",
1267 dispid.preferred_timings.size() > 1 ? "s" : "");
1268 for (vec_timings_ext::iterator iter = dispid.preferred_timings.begin();
1269 iter != dispid.preferred_timings.end(); ++iter)
1270 print_timings(" ", *iter, true, false);
1274 void edid_state::print_native_res()
1276 typedef std::pair<unsigned, unsigned> resolution;
1277 typedef std::set<resolution> resolution_set;
1278 resolution_set native_prog, native_int, native_nvrdb;
1279 unsigned native_width = 0, native_height = 0;
1280 unsigned native_width_int = 0, native_height_int = 0;
1282 // Note: it is also a mismatch if Block 0 does not define a
1283 // native resolution, but other blocks do.
1284 bool native_mismatch = false;
1285 bool native_int_mismatch = false;
1287 if (base.preferred_timing.is_valid() && base.preferred_is_also_native) {
1288 if (base.preferred_timing.t.interlaced) {
1289 native_width_int = base.preferred_timing.t.hact;
1290 native_height_int = base.preferred_timing.t.vact;
1291 } else {
1292 native_width = base.preferred_timing.t.hact;
1293 native_height = base.preferred_timing.t.vact;
1297 if (!native_width && dispid.native_width) {
1298 native_width = dispid.native_width;
1299 native_height = dispid.native_height;
1300 native_mismatch = true;
1301 } else if (dispid.native_width && native_width &&
1302 (dispid.native_width != native_width ||
1303 dispid.native_height != native_height)) {
1304 native_mismatch = true;
1307 for (vec_timings_ext::iterator iter = cta.native_timings.begin();
1308 iter != cta.native_timings.end(); ++iter) {
1309 if (iter->t.interlaced) {
1310 native_int.insert(std::pair<unsigned, unsigned>(iter->t.hact, iter->t.vact));
1311 if (!native_width_int) {
1312 native_width_int = iter->t.hact;
1313 native_height_int = iter->t.vact;
1314 native_int_mismatch = true;
1315 } else if (native_width_int &&
1316 (iter->t.hact != native_width_int ||
1317 iter->t.vact != native_height_int)) {
1318 native_int_mismatch = true;
1320 } else {
1321 native_prog.insert(std::pair<unsigned, unsigned>(iter->t.hact, iter->t.vact));
1322 if (!native_width) {
1323 native_width = iter->t.hact;
1324 native_height = iter->t.vact;
1325 native_mismatch = true;
1326 } else if (native_width &&
1327 (iter->t.hact != native_width ||
1328 iter->t.vact != native_height)) {
1329 native_mismatch = true;
1334 for (vec_timings_ext::iterator iter = cta.native_timing_nvrdb.begin();
1335 iter != cta.native_timing_nvrdb.end(); ++iter) {
1336 if (iter->t.interlaced) {
1337 fail("Interlaced native timing in NVRDB.\n");
1338 } else {
1339 native_nvrdb.insert(std::pair<unsigned, unsigned>(iter->t.hact, iter->t.vact));
1340 if (!native_width) {
1341 native_width = iter->t.hact;
1342 native_height = iter->t.vact;
1343 native_mismatch = true;
1344 } else if (native_width &&
1345 (iter->t.hact != native_width ||
1346 iter->t.vact != native_height)) {
1347 native_mismatch = true;
1352 if (diagonal) {
1353 if (image_width) {
1354 double w = image_width;
1355 double h = image_height;
1356 double d = sqrt(w * w + h * h) / 254.0;
1358 if (fabs(diagonal - d) >= 0.1)
1359 warn("Specified diagonal is %.1f\", calculated diagonal is %.1f\".\n",
1360 diagonal, d);
1362 if (native_width) {
1363 double w = native_width;
1364 double h = native_height;
1365 double d = diagonal * 254.0;
1366 double c = sqrt((d * d) / (w * w + h * h));
1368 w *= c;
1369 h *= c;
1371 if (image_width) {
1372 printf("\n----------------\n");
1373 printf("\nCalculated image size for a diagonal of %.1f\" is %.1fx%.1fmm.\n",
1374 diagonal, w / 10.0, h / 10.0);
1376 if (fabs((double)image_width - w) >= 100.0 ||
1377 fabs((double)image_height - h) >= 100.0)
1378 warn("Calculated image size is %.1fx%.1fmm, EDID image size is %.1fx%.1fmm.\n",
1379 w / 10.0, h / 10.0,
1380 image_width / 10.0, image_height / 10.0);
1381 } else {
1382 warn("No image size was specified, but it is calculated as %.1fx%.1fmm.\n",
1383 w / 10.0, h / 10.0);
1388 if (!options[OptNativeResolution])
1389 return;
1391 if (native_width == 0 && native_width_int == 0) {
1392 printf("\n----------------\n");
1393 printf("\nNo Native Video Resolution was defined.\n");
1394 return;
1397 if ((native_width || native_width_int) &&
1398 !native_mismatch && !native_int_mismatch) {
1399 printf("\n----------------\n");
1400 printf("\nNative Video Resolution%s:\n",
1401 native_width && native_width_int ? "s" : "");
1402 if (native_width)
1403 printf(" %ux%u\n", native_width, native_height);
1404 if (native_width_int)
1405 printf(" %ux%ui\n", native_width_int, native_height_int);
1406 return;
1409 if (base.preferred_timing.is_valid() && base.preferred_is_also_native) {
1410 printf("\n----------------\n");
1411 printf("\nNative Video Resolution if only Block 0 is parsed:\n");
1412 printf(" %ux%u%s\n",
1413 base.preferred_timing.t.hact, base.preferred_timing.t.vact,
1414 base.preferred_timing.t.interlaced ? "i" : "");
1417 if (!cta.native_timings.empty()) {
1418 printf("\n----------------\n");
1419 printf("\nNative Video Resolution%s if Block 0 and CTA-861 Blocks are parsed:\n",
1420 native_prog.size() + native_int.size() > 1 ? "s" : "");
1421 for (resolution_set::iterator iter = native_prog.begin();
1422 iter != native_prog.end(); ++iter)
1423 printf(" %ux%u\n", iter->first, iter->second);
1424 for (resolution_set::iterator iter = native_int.begin();
1425 iter != native_int.end(); ++iter)
1426 printf(" %ux%ui\n", iter->first, iter->second);
1429 if (!cta.native_timing_nvrdb.empty()) {
1430 printf("\n----------------\n");
1431 printf("\nNative Video Resolution if Block 0 and CTA-861 Blocks are parsed with NVRDB support:\n");
1432 for (resolution_set::iterator iter = native_nvrdb.begin();
1433 iter != native_nvrdb.end(); ++iter)
1434 printf(" %ux%u\n", iter->first, iter->second);
1437 if (dispid.native_width) {
1438 printf("\n----------------\n");
1439 printf("\nNative Video Resolution if the DisplayID Blocks are parsed:\n");
1440 printf(" %ux%u\n", dispid.native_width, dispid.native_height);
1444 int edid_state::parse_edid()
1446 hide_serial_numbers = options[OptHideSerialNumbers];
1447 replace_unique_ids = options[OptReplaceUniqueIDs];
1449 preparse_base_block(edid);
1450 if (replace_unique_ids)
1451 replace_checksum(edid, EDID_PAGE_SIZE);
1453 for (unsigned i = 1; i < num_blocks; i++)
1454 preparse_extension(edid + i * EDID_PAGE_SIZE);
1456 if (options[OptPhysicalAddress]) {
1457 printf("%x.%x.%x.%x\n",
1458 (cta.preparsed_phys_addr >> 12) & 0xf,
1459 (cta.preparsed_phys_addr >> 8) & 0xf,
1460 (cta.preparsed_phys_addr >> 4) & 0xf,
1461 cta.preparsed_phys_addr & 0xf);
1462 return 0;
1465 if (!options[OptSkipHexDump]) {
1466 printf("edid-decode (hex):\n\n");
1467 for (unsigned i = 0; i < num_blocks; i++) {
1468 hex_block("", edid + i * EDID_PAGE_SIZE, EDID_PAGE_SIZE, false);
1469 if (i == num_blocks - 1 && options[OptOnlyHexDump])
1470 return 0;
1471 printf("\n");
1473 printf("----------------\n\n");
1476 block = block_name(0x00);
1477 printf("Block %u, %s:\n", block_nr, block.c_str());
1478 parse_base_block(edid);
1480 for (unsigned i = 1; i < num_blocks; i++) {
1481 block_nr++;
1482 printf("\n----------------\n");
1483 parse_extension(edid + i * EDID_PAGE_SIZE);
1486 block = "";
1487 block_nr = EDID_MAX_BLOCKS;
1489 if (cta.has_svrs)
1490 cta_resolve_svrs();
1492 if (options[OptPreferredTimings])
1493 print_preferred_timings();
1495 print_native_res();
1497 if (!options[OptCheck] && !options[OptCheckInline])
1498 return 0;
1500 check_base_block(edid);
1501 if (has_cta)
1502 check_cta_blocks();
1503 if (has_dispid)
1504 check_displayid_blocks();
1506 printf("\n----------------\n");
1508 if (!options[OptSkipSHA] && strlen(STRING(SHA))) {
1509 printf("\nedid-decode SHA: %s %s\n", STRING(SHA), STRING(DATE));
1512 if (options[OptCheck]) {
1513 if (warnings)
1514 show_msgs(true);
1515 if (failures)
1516 show_msgs(false);
1518 printf("\nEDID conformity: %s\n", failures ? "FAIL" : "PASS");
1519 return failures ? -2 : 0;
1522 #ifndef __EMSCRIPTEN__
1524 static unsigned char crc_calc(const unsigned char *b)
1526 unsigned char sum = 0;
1527 unsigned i;
1529 for (i = 0; i < 127; i++)
1530 sum += b[i];
1531 return 256 - sum;
1534 static int crc_ok(const unsigned char *b)
1536 return crc_calc(b) == b[127];
1539 static void hexdumpedid(FILE *f, const unsigned char *edid, unsigned size)
1541 unsigned b, i, j;
1543 for (b = 0; b < size / 128; b++) {
1544 const unsigned char *buf = edid + 128 * b;
1546 if (b)
1547 fprintf(f, "\n");
1548 for (i = 0; i < 128; i += 0x10) {
1549 fprintf(f, "%02x", buf[i]);
1550 for (j = 1; j < 0x10; j++) {
1551 fprintf(f, " %02x", buf[i + j]);
1553 fprintf(f, "\n");
1555 if (!crc_ok(buf))
1556 fprintf(f, "Block %u has a checksum error (should be 0x%02x).\n",
1557 b, crc_calc(buf));
1561 static void carraydumpedid(FILE *f, const unsigned char *edid, unsigned size)
1563 unsigned b, i, j;
1565 fprintf(f, "const unsigned char edid[] = {\n");
1566 for (b = 0; b < size / 128; b++) {
1567 const unsigned char *buf = edid + 128 * b;
1569 if (b)
1570 fprintf(f, "\n");
1571 for (i = 0; i < 128; i += 8) {
1572 fprintf(f, "\t0x%02x,", buf[i]);
1573 for (j = 1; j < 8; j++) {
1574 fprintf(f, " 0x%02x,", buf[i + j]);
1576 fprintf(f, "\n");
1578 if (!crc_ok(buf))
1579 fprintf(f, "\t/* Block %u has a checksum error (should be 0x%02x). */\n",
1580 b, crc_calc(buf));
1582 fprintf(f, "};\n");
1585 // This format can be read by the QuantumData EDID editor
1586 static void xmldumpedid(FILE *f, const unsigned char *edid, unsigned size)
1588 fprintf(f, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
1589 fprintf(f, "<DATAOBJ>\n");
1590 fprintf(f, " <HEADER TYPE=\"DID\" VERSION=\"1.0\"/>\n");
1591 fprintf(f, " <DATA>\n");
1592 for (unsigned b = 0; b < size / 128; b++) {
1593 const unsigned char *buf = edid + 128 * b;
1595 fprintf(f, " <BLOCK%u>", b);
1596 for (unsigned i = 0; i < 128; i++)
1597 fprintf(f, "%02X", buf[i]);
1598 fprintf(f, "</BLOCK%u>\n", b);
1600 fprintf(f, " </DATA>\n");
1601 fprintf(f, "</DATAOBJ>\n");
1604 static int edid_to_file(const char *to_file, enum output_format out_fmt)
1606 FILE *out;
1608 if (!strcmp(to_file, "-")) {
1609 to_file = "stdout";
1610 out = stdout;
1611 } else if ((out = fopen(to_file, "w")) == NULL) {
1612 perror(to_file);
1613 return -1;
1615 if (out_fmt == OUT_FMT_DEFAULT)
1616 out_fmt = out == stdout ? OUT_FMT_HEX : OUT_FMT_RAW;
1618 switch (out_fmt) {
1619 default:
1620 case OUT_FMT_HEX:
1621 hexdumpedid(out, edid, state.edid_size);
1622 break;
1623 case OUT_FMT_RAW:
1624 fwrite(edid, state.edid_size, 1, out);
1625 break;
1626 case OUT_FMT_CARRAY:
1627 carraydumpedid(out, edid, state.edid_size);
1628 break;
1629 case OUT_FMT_XML:
1630 xmldumpedid(out, edid, state.edid_size);
1631 break;
1634 if (out != stdout)
1635 fclose(out);
1636 return 0;
1639 enum cvt_opts {
1640 CVT_WIDTH = 0,
1641 CVT_HEIGHT,
1642 CVT_FPS,
1643 CVT_INTERLACED,
1644 CVT_OVERSCAN,
1645 CVT_RB,
1646 CVT_ALT,
1647 CVT_RB_H_BLANK,
1648 CVT_RB_V_BLANK,
1649 CVT_EARLY_VSYNC,
1652 static int parse_cvt_subopt(char **subopt_str, double *value)
1654 int opt;
1655 char *opt_str;
1657 static const char * const subopt_list[] = {
1658 "w",
1659 "h",
1660 "fps",
1661 "interlaced",
1662 "overscan",
1663 "rb",
1664 "alt",
1665 "hblank",
1666 "vblank",
1667 "early-vsync",
1668 nullptr
1671 opt = getsubopt(subopt_str, (char* const*) subopt_list, &opt_str);
1673 if (opt == -1) {
1674 fprintf(stderr, "Invalid suboptions specified.\n");
1675 usage();
1676 std::exit(EXIT_FAILURE);
1678 if (opt_str == nullptr && opt != CVT_INTERLACED && opt != CVT_ALT &&
1679 opt != CVT_OVERSCAN && opt != CVT_EARLY_VSYNC) {
1680 fprintf(stderr, "No value given to suboption <%s>.\n",
1681 subopt_list[opt]);
1682 usage();
1683 std::exit(EXIT_FAILURE);
1686 if (opt_str)
1687 *value = strtod(opt_str, nullptr);
1688 return opt;
1691 static void parse_cvt(char *optarg)
1693 unsigned w = 0, h = 0;
1694 double fps = 0;
1695 unsigned rb = RB_NONE;
1696 unsigned rb_h_blank = 0;
1697 unsigned rb_v_blank = 460;
1698 bool interlaced = false;
1699 bool alt = false;
1700 bool overscan = false;
1701 bool early_vsync = false;
1703 while (*optarg != '\0') {
1704 int opt;
1705 double opt_val;
1707 opt = parse_cvt_subopt(&optarg, &opt_val);
1709 switch (opt) {
1710 case CVT_WIDTH:
1711 w = round(opt_val);
1712 break;
1713 case CVT_HEIGHT:
1714 h = round(opt_val);
1715 break;
1716 case CVT_FPS:
1717 fps = opt_val;
1718 break;
1719 case CVT_RB:
1720 rb = opt_val;
1721 break;
1722 case CVT_OVERSCAN:
1723 overscan = true;
1724 break;
1725 case CVT_INTERLACED:
1726 interlaced = opt_val;
1727 break;
1728 case CVT_ALT:
1729 alt = opt_val;
1730 break;
1731 case CVT_RB_H_BLANK:
1732 rb_h_blank = opt_val;
1733 break;
1734 case CVT_RB_V_BLANK:
1735 rb_v_blank = opt_val;
1736 if (rb_v_blank < 460) {
1737 fprintf(stderr, "vblank must be >= 460, set to 460.\n");
1738 rb_v_blank = 460;
1739 } else if (rb_v_blank > 705) {
1740 fprintf(stderr, "warning: vblank values > 705 might not be supported by RBv3 compliant sources.\n");
1742 break;
1743 case CVT_EARLY_VSYNC:
1744 early_vsync = true;
1745 break;
1746 default:
1747 break;
1751 if (!w || !h || !fps) {
1752 fprintf(stderr, "Missing width, height and/or fps.\n");
1753 usage();
1754 std::exit(EXIT_FAILURE);
1756 if (interlaced)
1757 fps /= 2;
1758 timings t = state.calc_cvt_mode(w, h, fps, rb, interlaced, overscan, alt,
1759 rb_h_blank, rb_v_blank, early_vsync);
1760 state.print_timings("", &t, "CVT", "", true, false);
1763 struct gtf_parsed_data {
1764 unsigned w, h;
1765 double freq;
1766 double C, M, K, J;
1767 bool overscan;
1768 bool interlaced;
1769 bool secondary;
1770 bool params_from_edid;
1771 enum gtf_ip_parm ip_parm;
1774 enum gtf_opts {
1775 GTF_WIDTH = 0,
1776 GTF_HEIGHT,
1777 GTF_FPS,
1778 GTF_HORFREQ,
1779 GTF_PIXCLK,
1780 GTF_INTERLACED,
1781 GTF_OVERSCAN,
1782 GTF_SECONDARY,
1783 GTF_C2,
1784 GTF_M,
1785 GTF_K,
1786 GTF_J2,
1789 static int parse_gtf_subopt(char **subopt_str, double *value)
1791 int opt;
1792 char *opt_str;
1794 static const char * const subopt_list[] = {
1795 "w",
1796 "h",
1797 "fps",
1798 "horfreq",
1799 "pixclk",
1800 "interlaced",
1801 "overscan",
1802 "secondary",
1803 "C",
1804 "M",
1805 "K",
1806 "J",
1807 nullptr
1810 opt = getsubopt(subopt_str, (char * const *)subopt_list, &opt_str);
1812 if (opt == -1) {
1813 fprintf(stderr, "Invalid suboptions specified.\n");
1814 usage();
1815 std::exit(EXIT_FAILURE);
1817 if (opt_str == nullptr && opt != GTF_INTERLACED && opt != GTF_OVERSCAN &&
1818 opt != GTF_SECONDARY) {
1819 fprintf(stderr, "No value given to suboption <%s>.\n",
1820 subopt_list[opt]);
1821 usage();
1822 std::exit(EXIT_FAILURE);
1825 if (opt == GTF_C2 || opt == GTF_J2)
1826 *value = round(2.0 * strtod(opt_str, nullptr));
1827 else if (opt_str)
1828 *value = strtod(opt_str, nullptr);
1829 return opt;
1832 static void parse_gtf(char *optarg, gtf_parsed_data &data)
1834 memset(&data, 0, sizeof(data));
1835 data.params_from_edid = true;
1836 data.C = 40;
1837 data.M = 600;
1838 data.K = 128;
1839 data.J = 20;
1841 while (*optarg != '\0') {
1842 int opt;
1843 double opt_val;
1845 opt = parse_gtf_subopt(&optarg, &opt_val);
1847 switch (opt) {
1848 case GTF_WIDTH:
1849 data.w = round(opt_val);
1850 break;
1851 case GTF_HEIGHT:
1852 data.h = round(opt_val);
1853 break;
1854 case GTF_FPS:
1855 data.freq = opt_val;
1856 data.ip_parm = gtf_ip_vert_freq;
1857 break;
1858 case GTF_HORFREQ:
1859 data.freq = opt_val;
1860 data.ip_parm = gtf_ip_hor_freq;
1861 break;
1862 case GTF_PIXCLK:
1863 data.freq = opt_val;
1864 data.ip_parm = gtf_ip_clk_freq;
1865 break;
1866 case GTF_INTERLACED:
1867 data.interlaced = true;
1868 break;
1869 case GTF_OVERSCAN:
1870 data.overscan = true;
1871 break;
1872 case GTF_SECONDARY:
1873 data.secondary = true;
1874 break;
1875 case GTF_C2:
1876 data.C = opt_val / 2.0;
1877 data.params_from_edid = false;
1878 break;
1879 case GTF_M:
1880 data.M = round(opt_val);
1881 data.params_from_edid = false;
1882 break;
1883 case GTF_K:
1884 data.K = round(opt_val);
1885 data.params_from_edid = false;
1886 break;
1887 case GTF_J2:
1888 data.J = opt_val / 2.0;
1889 data.params_from_edid = false;
1890 break;
1891 default:
1892 break;
1896 if (!data.w || !data.h) {
1897 fprintf(stderr, "Missing width and/or height.\n");
1898 usage();
1899 std::exit(EXIT_FAILURE);
1901 if (!data.freq) {
1902 fprintf(stderr, "One of fps, horfreq or pixclk must be given.\n");
1903 usage();
1904 std::exit(EXIT_FAILURE);
1906 if (!data.secondary)
1907 data.params_from_edid = false;
1908 if (data.interlaced && data.ip_parm == gtf_ip_vert_freq)
1909 data.freq /= 2;
1912 static void show_gtf(gtf_parsed_data &data)
1914 timings t;
1916 t = state.calc_gtf_mode(data.w, data.h, data.freq, data.interlaced,
1917 data.ip_parm, data.overscan, data.secondary,
1918 data.C, data.M, data.K, data.J);
1919 calc_ratio(&t);
1920 state.print_timings("", &t, "GTF", "", true, false);
1923 enum ovt_opts {
1924 OVT_RID,
1925 OVT_WIDTH,
1926 OVT_HEIGHT,
1927 OVT_FPS,
1930 static int parse_ovt_subopt(char **subopt_str, unsigned *value)
1932 int opt;
1933 char *opt_str;
1935 static const char * const subopt_list[] = {
1936 "rid",
1937 "w",
1938 "h",
1939 "fps",
1940 nullptr
1943 opt = getsubopt(subopt_str, (char* const*) subopt_list, &opt_str);
1945 if (opt == -1) {
1946 fprintf(stderr, "Invalid suboptions specified.\n");
1947 usage();
1948 std::exit(EXIT_FAILURE);
1950 if (opt_str == nullptr) {
1951 fprintf(stderr, "No value given to suboption <%s>.\n",
1952 subopt_list[opt]);
1953 usage();
1954 std::exit(EXIT_FAILURE);
1957 if (opt_str)
1958 *value = strtoul(opt_str, NULL, 0);
1959 return opt;
1962 static void parse_ovt(char *optarg)
1964 unsigned rid = 0;
1965 unsigned w = 0, h = 0;
1966 unsigned fps = 0;
1968 while (*optarg != '\0') {
1969 int opt;
1970 unsigned opt_val;
1972 opt = parse_ovt_subopt(&optarg, &opt_val);
1974 switch (opt) {
1975 case OVT_RID:
1976 rid = opt_val;
1977 break;
1978 case OVT_WIDTH:
1979 w = opt_val;
1980 break;
1981 case OVT_HEIGHT:
1982 h = opt_val;
1983 break;
1984 case OVT_FPS:
1985 fps = opt_val;
1986 break;
1987 default:
1988 break;
1992 if ((!rid && (!w || !h)) || !fps) {
1993 fprintf(stderr, "Missing rid, width, height and/or fps.\n");
1994 usage();
1995 std::exit(EXIT_FAILURE);
1997 unsigned hratio = 0, vratio = 0;
1998 if (rid) {
1999 const cta_rid *r = find_rid(rid);
2001 if (r) {
2002 w = r->hact;
2003 h = r->vact;
2004 hratio = r->hratio;
2005 vratio = r->vratio;
2008 timings t = state.calc_ovt_mode(w, h, hratio, vratio, fps);
2009 state.print_timings("", &t, "OVT", "", true, false);
2012 int main(int argc, char **argv)
2014 char short_options[26 * 2 * 2 + 1];
2015 enum output_format out_fmt = OUT_FMT_DEFAULT;
2016 gtf_parsed_data gtf_data;
2017 unsigned list_rid = 0;
2018 int ret;
2020 while (1) {
2021 int option_index = 0;
2022 unsigned idx = 0;
2023 unsigned i, val;
2024 const timings *t;
2025 char buf[16];
2027 for (i = 0; long_options[i].name; i++) {
2028 if (!isalpha(long_options[i].val))
2029 continue;
2030 short_options[idx++] = long_options[i].val;
2031 if (long_options[i].has_arg == required_argument)
2032 short_options[idx++] = ':';
2034 short_options[idx] = 0;
2035 int ch = getopt_long(argc, argv, short_options,
2036 long_options, &option_index);
2037 if (ch == -1)
2038 break;
2040 options[ch] = 1;
2041 switch (ch) {
2042 case OptHelp:
2043 usage();
2044 return -1;
2045 case OptOutputFormat:
2046 if (!strcmp(optarg, "hex")) {
2047 out_fmt = OUT_FMT_HEX;
2048 } else if (!strcmp(optarg, "raw")) {
2049 out_fmt = OUT_FMT_RAW;
2050 } else if (!strcmp(optarg, "carray")) {
2051 out_fmt = OUT_FMT_CARRAY;
2052 } else if (!strcmp(optarg, "xml")) {
2053 out_fmt = OUT_FMT_XML;
2054 } else {
2055 usage();
2056 exit(1);
2058 break;
2059 case OptDiag:
2060 state.diagonal = strtod(optarg, NULL);
2061 break;
2062 case OptSTD: {
2063 unsigned char byte1, byte2 = 0;
2064 char *endptr;
2066 byte1 = strtoul(optarg, &endptr, 0);
2067 if (*endptr == ',')
2068 byte2 = strtoul(endptr + 1, NULL, 0);
2069 state.print_standard_timing("", byte1, byte2, false, true);
2070 break;
2072 case OptDMT:
2073 val = strtoul(optarg, NULL, 0);
2074 t = find_dmt_id(val);
2075 if (t) {
2076 sprintf(buf, "DMT 0x%02x", val);
2077 state.print_timings("", t, buf, "", true, false);
2078 } else {
2079 fprintf(stderr, "Unknown DMT code 0x%02x.\n", val);
2081 break;
2082 case OptVIC:
2083 val = strtoul(optarg, NULL, 0);
2084 t = find_vic_id(val);
2085 if (t) {
2086 sprintf(buf, "VIC %3u", val);
2087 state.print_timings("", t, buf, "", true, false);
2088 } else {
2089 fprintf(stderr, "Unknown VIC code %u.\n", val);
2091 break;
2092 case OptHDMIVIC:
2093 val = strtoul(optarg, NULL, 0);
2094 t = find_hdmi_vic_id(val);
2095 if (t) {
2096 sprintf(buf, "HDMI VIC %u", val);
2097 state.print_timings("", t, buf, "", true, false);
2098 } else {
2099 fprintf(stderr, "Unknown HDMI VIC code %u.\n", val);
2101 break;
2102 case OptCVT:
2103 parse_cvt(optarg);
2104 break;
2105 case OptGTF:
2106 parse_gtf(optarg, gtf_data);
2107 break;
2108 case OptOVT:
2109 parse_ovt(optarg);
2110 break;
2111 case OptListRIDTimings:
2112 list_rid = strtoul(optarg, NULL, 0);
2113 break;
2114 case ':':
2115 fprintf(stderr, "Option '%s' requires a value.\n",
2116 argv[optind]);
2117 usage();
2118 return -1;
2119 case '?':
2120 fprintf(stderr, "Unknown argument '%s'.\n",
2121 argv[optind]);
2122 usage();
2123 return -1;
2126 if (optind == argc && options[OptVersion]) {
2127 if (strlen(STRING(SHA)))
2128 printf("edid-decode SHA: %s %s\n", STRING(SHA), STRING(DATE));
2129 else
2130 printf("edid-decode SHA: not available\n");
2131 return 0;
2134 if (options[OptListEstTimings])
2135 state.list_established_timings();
2136 if (options[OptListDMTs])
2137 state.list_dmts();
2138 if (options[OptListVICs])
2139 state.cta_list_vics();
2140 if (options[OptListHDMIVICs])
2141 state.cta_list_hdmi_vics();
2142 if (options[OptListRIDs])
2143 state.cta_list_rids();
2144 if (options[OptListRIDTimings])
2145 state.cta_list_rid_timings(list_rid);
2147 if (options[OptListEstTimings] || options[OptListDMTs] ||
2148 options[OptListVICs] || options[OptListHDMIVICs] ||
2149 options[OptListRIDs] || options[OptListRIDTimings])
2150 return 0;
2152 if (options[OptCVT] || options[OptDMT] || options[OptVIC] ||
2153 options[OptHDMIVIC] || options[OptSTD] || options[OptOVT])
2154 return 0;
2156 if (options[OptGTF] && (!gtf_data.params_from_edid || optind == argc)) {
2157 show_gtf(gtf_data);
2158 return 0;
2161 if (optind == argc)
2162 ret = edid_from_file("-", stdout);
2163 else
2164 ret = edid_from_file(argv[optind], argv[optind + 1] ? stderr : stdout);
2166 if (ret && options[OptPhysicalAddress]) {
2167 printf("f.f.f.f\n");
2168 return 0;
2170 if (optind < argc - 1)
2171 return ret ? ret : edid_to_file(argv[optind + 1], out_fmt);
2173 if (options[OptGTF]) {
2174 timings t;
2176 state.preparse_base_block(edid);
2178 t = state.calc_gtf_mode(gtf_data.w, gtf_data.h, gtf_data.freq,
2179 gtf_data.interlaced, gtf_data.ip_parm,
2180 gtf_data.overscan);
2181 unsigned hbl = t.hfp + t.hsync + t.hbp;
2182 unsigned htotal = t.hact + hbl;
2183 double hor_freq_khz = htotal ? (double)t.pixclk_khz / htotal : 0;
2185 if (state.base.supports_sec_gtf &&
2186 hor_freq_khz >= state.base.sec_gtf_start_freq) {
2187 t = state.calc_gtf_mode(gtf_data.w, gtf_data.h, gtf_data.freq,
2188 gtf_data.interlaced, gtf_data.ip_parm,
2189 gtf_data.overscan, true,
2190 state.base.C, state.base.M,
2191 state.base.K, state.base.J);
2193 calc_ratio(&t);
2194 if (t.hfp <= 0)
2195 state.print_timings("", &t, "GTF", "INVALID: Hfront <= 0", true, false);
2196 else
2197 state.print_timings("", &t, "GTF", "", true, false);
2198 return 0;
2201 return ret ? ret : state.parse_edid();
2204 #else
2207 * The surrounding JavaScript implementation will call this function
2208 * each time it wants to decode an EDID. So this should reset all the
2209 * state and start over.
2211 extern "C" int parse_edid(const char *input)
2213 for (unsigned i = 0; i < EDID_MAX_BLOCKS + 1; i++) {
2214 s_msgs[i][0].clear();
2215 s_msgs[i][1].clear();
2217 options[OptCheck] = 1;
2218 options[OptPreferredTimings] = 1;
2219 options[OptNativeResolution] = 1;
2220 state = edid_state();
2221 int ret = edid_from_file(input, stderr);
2222 return ret ? ret : state.parse_edid();
2225 #endif