edid-decode: update to CTA-861-I
[edid-decode.git] / edid-decode.cpp
bloba30165319cac3f109d2352fae76d335211567210
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, 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 edid_state::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 if (vic_t)
641 warn("DTD is similar but not identical to VIC %u.\n", vic);
643 const timings *dmt_t = close_match_to_dmt(*t, dmt);
644 if (!vic_t && dmt_t)
645 warn("DTD is similar but not identical to DMT 0x%02x.\n", dmt);
648 if (refresh) {
649 min_vert_freq_hz = min(min_vert_freq_hz, refresh);
650 max_vert_freq_hz = max(max_vert_freq_hz, refresh);
652 if (hor_freq_khz) {
653 min_hor_freq_hz = min(min_hor_freq_hz, hor_freq_khz * 1000.0);
654 max_hor_freq_hz = max(max_hor_freq_hz, hor_freq_khz * 1000.0);
655 max_pixclk_khz = max(max_pixclk_khz, pixclk_khz);
656 if (t->pos_pol_hsync && !t->pos_pol_vsync && t->vsync == 3)
657 base.max_pos_neg_hor_freq_khz = hor_freq_khz;
660 if (t->ycbcr420 && t->pixclk_khz < 590000)
661 warn_once("Some YCbCr 4:2:0 timings are invalid for HDMI 2.1 (which requires an RGB timings pixel rate >= 590 MHz).\n");
662 if (t->hfp <= 0)
663 fail("0 or negative horizontal front porch.\n");
664 if (t->hbp <= 0)
665 fail("0 or negative horizontal back porch.\n");
666 if (t->vbp <= 0)
667 fail("0 or negative vertical back porch.\n");
668 if (!base.max_display_width_mm && !base.max_display_height_mm) {
669 /* this is valid */
670 } else if (!t->hsize_mm && !t->vsize_mm) {
671 /* this is valid */
672 } else if (t->hsize_mm > base.max_display_width_mm + 9 ||
673 t->vsize_mm > base.max_display_height_mm + 9) {
674 fail("Mismatch of image size %ux%u mm vs display size %ux%u mm.\n",
675 t->hsize_mm, t->vsize_mm, base.max_display_width_mm, base.max_display_height_mm);
676 } else if (t->hsize_mm < base.max_display_width_mm - 9 &&
677 t->vsize_mm < base.max_display_height_mm - 9) {
678 fail("Mismatch of image size %ux%u mm vs display size %ux%u mm.\n",
679 t->hsize_mm, t->vsize_mm, base.max_display_width_mm, base.max_display_height_mm);
681 return ok;
684 std::string containerid2s(const unsigned char *x)
686 char buf[40];
688 sprintf(buf, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
689 x[0], x[1], x[2], x[3],
690 x[4], x[5],
691 x[6], x[7],
692 x[8], x[9],
693 x[10], x[11], x[12], x[13], x[14], x[15]);
694 return buf;
697 std::string utohex(unsigned char x)
699 char buf[10];
701 sprintf(buf, "0x%02hhx", x);
702 return buf;
705 const char *oui_name(unsigned oui, unsigned *ouinum)
707 unsigned ouinumscratch;
708 if (!ouinum) ouinum = &ouinumscratch;
709 const char *name;
710 switch (oui) {
711 #define oneoui(c,k,n) case c: *ouinum = kOUI_##k; name = n; break;
712 #include "oui.h"
713 default: *ouinum = 0; name = NULL; break;
715 return name;
718 void edid_state::data_block_oui(std::string block_name, const unsigned char *x,
719 unsigned length, unsigned *ouinum, bool ignorezeros, bool do_ascii, bool big_endian)
721 std::string buf;
722 char ascii[4];
723 unsigned oui;
724 const char *ouiname = NULL;
725 bool matched_reverse = false;
726 bool matched_ascii = false;
727 bool valid_ascii = false;
729 if (big_endian)
730 oui = ((length > 0 ? x[0] : 0) << 16) + ((length > 1 ? x[1] : 0) << 8) + (length > 2 ? x[2] : 0);
731 else
732 oui = ((length > 2 ? x[2] : 0) << 16) + ((length > 1 ? x[1] : 0) << 8) + (length > 0 ? x[0] : 0);
734 buf = ouitohex(oui);
735 if (length < 3) {
736 sprintf(ascii, "?"); // some characters are null
737 if (ouinum) *ouinum = 0; // doesn't match a known OUI
738 } else {
739 valid_ascii = (x[0] >= 'A' && x[1] >= 'A' && x[2] >= 'A' && x[0] <= 'Z' && x[1] <= 'Z' && x[2] <= 'Z');
740 sprintf(ascii, "%c%c%c", x[0], x[1], x[2]);
742 ouiname = oui_name(oui, ouinum);
743 if (!ouiname) {
744 big_endian = !big_endian;
745 unsigned reversedoui = ((oui & 0xff) << 16) + (oui & 0x00ff00) + (oui >> 16);
746 ouiname = oui_name(reversedoui, ouinum);
747 if (ouiname) {
748 oui = reversedoui;
749 buf = ouitohex(oui);
750 matched_reverse = true;
751 } else if (do_ascii && valid_ascii) {
752 unsigned asciioui = (x[0] << 24) + (x[1] << 16) + (x[2] << 8);
753 ouiname = oui_name(asciioui, ouinum);
754 if (ouiname) {
755 matched_ascii = true;
761 std::string name;
762 if (ouiname) {
763 if (matched_ascii)
764 name = block_name + " (" + ouiname + ")" + ", PNP ID '" + ascii + "'";
765 else
766 name = block_name + " (" + ouiname + ")" + ", OUI " + buf;
767 } else if (do_ascii && valid_ascii) {
768 name = block_name + ", PNP ID '" + ascii + "'";
769 } else {
770 name = block_name + ", OUI " + buf;
772 // assign string to data_block before outputting errors
773 data_block = name;
775 if (oui || !ignorezeros) {
776 printf(" %s:\n", data_block.c_str());
777 if (length < 3)
778 fail("Data block length (%d) is not enough to contain an OUI.\n", length);
779 else if (ouiname) {
780 if (do_ascii && !valid_ascii)
781 warn("Expected PNP ID but found OUI.\n");
782 if (matched_reverse)
783 fail("Endian-ness (%s) of OUI is different than expected (%s).\n", big_endian ? "be" : "le", big_endian ? "le" : "be");
785 else {
786 if (valid_ascii)
787 warn("Unknown OUI %s (possible PNP %s).\n", buf.c_str(), ascii);
788 else
789 warn("Unknown OUI %s.\n", buf.c_str());
794 std::string ouitohex(unsigned oui)
796 char buf[32];
798 sprintf(buf, "%02X-%02X-%02X", (oui >> 16) & 0xff, (oui >> 8) & 0xff, oui & 0xff);
799 return buf;
802 bool memchk(const unsigned char *x, unsigned len, unsigned char v)
804 for (unsigned i = 0; i < len; i++)
805 if (x[i] != v)
806 return false;
807 return true;
810 void hex_block(const char *prefix, const unsigned char *x,
811 unsigned length, bool show_ascii, unsigned step)
813 unsigned i, j;
815 for (i = 0; i < length; i += step) {
816 unsigned len = min(step, length - i);
818 printf("%s", prefix);
819 for (j = 0; j < len; j++)
820 printf("%s%02x", j ? " " : "", x[i + j]);
822 if (show_ascii) {
823 for (j = len; j < step; j++)
824 printf(" ");
825 printf(" '");
826 for (j = 0; j < len; j++)
827 printf("%c", x[i + j] >= ' ' && x[i + j] <= '~' ? x[i + j] : '.');
828 printf("'");
830 printf("\n");
834 static bool edid_add_byte(const char *s, bool two_digits = true)
836 char buf[3];
838 if (state.edid_size == sizeof(edid))
839 return false;
840 buf[0] = s[0];
841 buf[1] = two_digits ? s[1] : 0;
842 buf[2] = 0;
843 edid[state.edid_size++] = strtoul(buf, NULL, 16);
844 return true;
847 static bool extract_edid_quantumdata(const char *start)
849 /* Parse QuantumData 980 EDID files */
850 do {
851 start = strstr(start, ">");
852 if (!start)
853 return false;
854 start++;
855 for (unsigned i = 0; start[i] && start[i + 1] && i < 256; i += 2)
856 if (!edid_add_byte(start + i))
857 return false;
858 start = strstr(start, "<BLOCK");
859 } while (start);
860 return state.edid_size;
863 static const char *ignore_chars = ",:;";
865 static bool extract_edid_hex(const char *s, bool require_two_digits = true)
867 for (; *s; s++) {
868 if (isspace(*s) || strchr(ignore_chars, *s))
869 continue;
871 if (*s == '0' && tolower(s[1]) == 'x') {
872 s++;
873 continue;
876 /* Read one or two hex digits from the log */
877 if (!isxdigit(s[0])) {
878 if (state.edid_size && state.edid_size % 128 == 0)
879 break;
880 return false;
882 if (require_two_digits && !isxdigit(s[1])) {
883 odd_hex_digits = true;
884 return false;
886 if (!edid_add_byte(s, isxdigit(s[1])))
887 return false;
888 if (isxdigit(s[1]))
889 s++;
891 return state.edid_size;
894 static bool extract_edid_xrandr(const char *start)
896 static const char indentation1[] = " ";
897 static const char indentation2[] = "\t\t";
898 /* Used to detect that we've gone past the EDID property */
899 static const char half_indentation1[] = " ";
900 static const char half_indentation2[] = "\t";
901 const char *indentation;
902 const char *s;
904 for (;;) {
905 unsigned j;
907 /* Get the next start of the line of EDID hex, assuming spaces for indentation */
908 s = strstr(start, indentation = indentation1);
909 /* Did we skip the start of another property? */
910 if (s && s > strstr(start, half_indentation1))
911 break;
913 /* If we failed, retry assuming tabs for indentation */
914 if (!s) {
915 s = strstr(start, indentation = indentation2);
916 /* Did we skip the start of another property? */
917 if (s && s > strstr(start, half_indentation2))
918 break;
921 if (!s)
922 break;
924 start = s + strlen(indentation);
926 for (j = 0; j < 16; j++, start += 2) {
927 /* Read a %02x from the log */
928 if (!isxdigit(start[0]) || !isxdigit(start[1])) {
929 if (j)
930 break;
931 return false;
933 if (!edid_add_byte(start))
934 return false;
937 return state.edid_size;
940 static bool extract_edid_xorg(const char *start)
942 bool find_first_num = true;
944 for (; *start; start++) {
945 if (find_first_num) {
946 const char *s;
948 /* skip ahead to the : */
949 s = strstr(start, ": \t");
950 if (!s)
951 s = strstr(start, ": ");
952 if (!s)
953 break;
954 start = s;
955 /* and find the first number */
956 while (!isxdigit(start[1]))
957 start++;
958 find_first_num = false;
959 continue;
960 } else {
961 /* Read a %02x from the log */
962 if (!isxdigit(*start)) {
963 find_first_num = true;
964 continue;
966 if (!edid_add_byte(start))
967 return false;
968 start++;
971 return state.edid_size;
974 static bool extract_edid(int fd, FILE *error)
976 std::vector<char> edid_data;
977 char buf[EDID_PAGE_SIZE];
979 for (;;) {
980 ssize_t i = read(fd, buf, sizeof(buf));
982 if (i < 0)
983 return false;
984 if (i == 0)
985 break;
986 edid_data.insert(edid_data.end(), buf, buf + i);
989 if (edid_data.empty()) {
990 state.edid_size = 0;
991 return false;
994 const char *data = &edid_data[0];
995 const char *start;
997 /* Look for edid-decode output */
998 start = strstr(data, "EDID (hex):");
999 if (!start)
1000 start = strstr(data, "edid-decode (hex):");
1001 if (start)
1002 return extract_edid_hex(strchr(start, ':'));
1004 /* Look for C-array */
1005 start = strstr(data, "unsigned char edid[] = {");
1006 if (start)
1007 return extract_edid_hex(strchr(start, '{') + 1, false);
1009 /* Look for QuantumData EDID output */
1010 start = strstr(data, "<BLOCK");
1011 if (start)
1012 return extract_edid_quantumdata(start);
1014 /* Look for xrandr --verbose output (lines of 16 hex bytes) */
1015 start = strstr(data, "EDID_DATA:");
1016 if (!start)
1017 start = strstr(data, "EDID:");
1018 if (start)
1019 return extract_edid_xrandr(start);
1021 /* Look for an EDID in an Xorg.0.log file */
1022 start = strstr(data, "EDID (in hex):");
1023 if (start)
1024 start = strstr(start, "(II)");
1025 if (start)
1026 return extract_edid_xorg(start);
1028 unsigned i;
1030 /* Is the EDID provided in hex? */
1031 for (i = 0; i < 32 && (isspace(data[i]) || strchr(ignore_chars, data[i]) ||
1032 tolower(data[i]) == 'x' || isxdigit(data[i])); i++);
1034 if (i == 32)
1035 return extract_edid_hex(data);
1037 /* Assume binary */
1038 if (edid_data.size() > sizeof(edid)) {
1039 fprintf(error, "Binary EDID length %zu is greater than %zu.\n",
1040 edid_data.size(), sizeof(edid));
1041 return false;
1043 memcpy(edid, data, edid_data.size());
1044 state.edid_size = edid_data.size();
1045 return true;
1048 static int edid_from_file(const char *from_file, FILE *error)
1050 #ifdef O_BINARY
1051 // Windows compatibility
1052 int flags = O_RDONLY | O_BINARY;
1053 #else
1054 int flags = O_RDONLY;
1055 #endif
1056 int fd;
1058 if (!strcmp(from_file, "-")) {
1059 from_file = "stdin";
1060 fd = 0;
1061 } else if ((fd = open(from_file, flags)) == -1) {
1062 perror(from_file);
1063 return -1;
1066 odd_hex_digits = false;
1067 if (!extract_edid(fd, error)) {
1068 if (!state.edid_size) {
1069 fprintf(error, "EDID of '%s' was empty.\n", from_file);
1070 return -1;
1072 fprintf(error, "EDID extract of '%s' failed: ", from_file);
1073 if (odd_hex_digits)
1074 fprintf(error, "odd number of hexadecimal digits.\n");
1075 else
1076 fprintf(error, "unknown format.\n");
1077 return -1;
1079 if (state.edid_size % EDID_PAGE_SIZE) {
1080 fprintf(error, "EDID length %u is not a multiple of %u.\n",
1081 state.edid_size, EDID_PAGE_SIZE);
1082 return -1;
1084 state.num_blocks = state.edid_size / EDID_PAGE_SIZE;
1085 if (fd != 0)
1086 close(fd);
1088 if (memcmp(edid, "\x00\xFF\xFF\xFF\xFF\xFF\xFF\x00", 8)) {
1089 fprintf(error, "No EDID header found in '%s'.\n", from_file);
1090 return -1;
1092 return 0;
1095 /* generic extension code */
1097 std::string block_name(unsigned char block)
1099 char buf[10];
1101 switch (block) {
1102 case 0x00: return "Base EDID";
1103 case 0x02: return "CTA-861 Extension Block";
1104 case 0x10: return "Video Timing Extension Block";
1105 case 0x20: return "EDID 2.0 Extension Block";
1106 case 0x40: return "Display Information Extension Block";
1107 case 0x50: return "Localized String Extension Block";
1108 case 0x60: return "Microdisplay Interface Extension Block";
1109 case 0x70: return "DisplayID Extension Block";
1110 case 0xf0: return "Block Map Extension Block";
1111 case 0xff: return "Manufacturer-Specific Extension Block";
1112 default:
1113 sprintf(buf, " 0x%02x", block);
1114 return std::string("Unknown EDID Extension Block") + buf;
1118 void edid_state::parse_block_map(const unsigned char *x)
1120 unsigned last_valid_block_tag = 0;
1121 bool fail_once = false;
1122 unsigned offset = 1;
1123 unsigned i;
1125 if (block_nr == 1)
1126 block_map.saw_block_1 = true;
1127 else if (!block_map.saw_block_1)
1128 fail("No EDID Block Map Extension found in block 1.\n");
1129 else if (block_nr == 128)
1130 block_map.saw_block_128 = true;
1132 if (block_nr > 1)
1133 offset = 128;
1135 for (i = 1; i < 127; i++) {
1136 unsigned block = offset + i;
1138 if (x[i]) {
1139 last_valid_block_tag++;
1140 if (i != last_valid_block_tag && !fail_once) {
1141 fail("Valid block tags are not consecutive.\n");
1142 fail_once = true;
1144 printf(" Block %3u: %s\n", block, block_name(x[i]).c_str());
1145 if (block >= num_blocks) {
1146 if (!fail_once)
1147 fail("Invalid block number %u.\n", block);
1148 fail_once = true;
1149 } else if (x[i] != edid[block * EDID_PAGE_SIZE]) {
1150 fail("Block %u tag mismatch: expected 0x%02x, but got 0x%02x.\n",
1151 block, edid[block * EDID_PAGE_SIZE], x[i]);
1153 } else if (block < num_blocks) {
1154 fail("Block %u tag mismatch: expected 0x%02x, but got 0x00.\n",
1155 block, edid[block * EDID_PAGE_SIZE]);
1160 void edid_state::preparse_extension(unsigned char *x)
1162 switch (x[0]) {
1163 case 0x02:
1164 has_cta = true;
1165 preparse_cta_block(x);
1166 break;
1167 case 0x50:
1168 preparse_ls_ext_block(x);
1169 break;
1170 case 0x70:
1171 has_dispid = true;
1172 preparse_displayid_block(x);
1173 break;
1177 void edid_state::parse_extension(const unsigned char *x)
1179 block = block_name(x[0]);
1180 data_block.clear();
1181 unused_bytes = 0;
1183 printf("\n");
1184 if (block_nr && x[0] == 0)
1185 block = "Unknown EDID Extension Block 0x00";
1186 printf("Block %u, %s:\n", block_nr, block.c_str());
1188 switch (x[0]) {
1189 case 0x02:
1190 parse_cta_block(x);
1191 break;
1192 case 0x10:
1193 parse_vtb_ext_block(x);
1194 break;
1195 case 0x20:
1196 fail("Deprecated extension block for EDID 2.0, do not use.\n");
1197 break;
1198 case 0x40:
1199 parse_di_ext_block(x);
1200 break;
1201 case 0x50:
1202 parse_ls_ext_block(x);
1203 break;
1204 case 0x70:
1205 parse_displayid_block(x);
1206 break;
1207 case 0xf0:
1208 parse_block_map(x);
1209 if (block_nr != 1 && block_nr != 128)
1210 fail("Must be used in block 1 and 128.\n");
1211 break;
1212 default:
1213 hex_block(" ", x, EDID_PAGE_SIZE);
1214 fail("Unknown Extension Block.\n");
1215 break;
1218 data_block.clear();
1219 do_checksum("", x, EDID_PAGE_SIZE, unused_bytes);
1222 void edid_state::print_preferred_timings()
1224 if (base.preferred_timing.is_valid()) {
1225 printf("\n----------------\n");
1226 printf("\nPreferred Video Timing if only Block 0 is parsed:\n");
1227 print_timings(" ", base.preferred_timing, true, false);
1230 if (!cta.preferred_timings.empty()) {
1231 printf("\n----------------\n");
1232 printf("\nPreferred Video Timing%s if Block 0 and CTA-861 Blocks are parsed:\n",
1233 cta.preferred_timings.size() > 1 ? "s" : "");
1234 for (vec_timings_ext::iterator iter = cta.preferred_timings.begin();
1235 iter != cta.preferred_timings.end(); ++iter)
1236 print_timings(" ", *iter, true, false);
1239 if (!cta.preferred_timings_vfpdb.empty()) {
1240 printf("\n----------------\n");
1241 printf("\nPreferred Video Timing%s if Block 0 and CTA-861 Blocks are parsed with VFPDB support:\n",
1242 cta.preferred_timings_vfpdb.size() > 1 ? "s" : "");
1243 for (vec_timings_ext::iterator iter = cta.preferred_timings_vfpdb.begin();
1244 iter != cta.preferred_timings_vfpdb.end(); ++iter)
1245 print_timings(" ", *iter, true, false);
1248 if (!dispid.preferred_timings.empty()) {
1249 printf("\n----------------\n");
1250 printf("\nPreferred Video Timing%s if Block 0 and DisplayID Blocks are parsed:\n",
1251 dispid.preferred_timings.size() > 1 ? "s" : "");
1252 for (vec_timings_ext::iterator iter = dispid.preferred_timings.begin();
1253 iter != dispid.preferred_timings.end(); ++iter)
1254 print_timings(" ", *iter, true, false);
1258 void edid_state::print_native_res()
1260 typedef std::pair<unsigned, unsigned> resolution;
1261 typedef std::set<resolution> resolution_set;
1262 resolution_set native_prog, native_int, native_nvrdb;
1263 unsigned native_width = 0, native_height = 0;
1264 unsigned native_width_int = 0, native_height_int = 0;
1266 // Note: it is also a mismatch if Block 0 does not define a
1267 // native resolution, but other blocks do.
1268 bool native_mismatch = false;
1269 bool native_int_mismatch = false;
1271 if (base.preferred_timing.is_valid() && base.preferred_is_also_native) {
1272 if (base.preferred_timing.t.interlaced) {
1273 native_width_int = base.preferred_timing.t.hact;
1274 native_height_int = base.preferred_timing.t.vact;
1275 } else {
1276 native_width = base.preferred_timing.t.hact;
1277 native_height = base.preferred_timing.t.vact;
1281 if (!native_width && dispid.native_width) {
1282 native_width = dispid.native_width;
1283 native_height = dispid.native_height;
1284 native_mismatch = true;
1285 } else if (dispid.native_width && native_width &&
1286 (dispid.native_width != native_width ||
1287 dispid.native_height != native_height)) {
1288 native_mismatch = true;
1291 for (vec_timings_ext::iterator iter = cta.native_timings.begin();
1292 iter != cta.native_timings.end(); ++iter) {
1293 if (iter->t.interlaced) {
1294 native_int.insert(std::pair<unsigned, unsigned>(iter->t.hact, iter->t.vact));
1295 if (!native_width_int) {
1296 native_width_int = iter->t.hact;
1297 native_height_int = iter->t.vact;
1298 native_int_mismatch = true;
1299 } else if (native_width_int &&
1300 (iter->t.hact != native_width_int ||
1301 iter->t.vact != native_height_int)) {
1302 native_int_mismatch = true;
1304 } else {
1305 native_prog.insert(std::pair<unsigned, unsigned>(iter->t.hact, iter->t.vact));
1306 if (!native_width) {
1307 native_width = iter->t.hact;
1308 native_height = iter->t.vact;
1309 native_mismatch = true;
1310 } else if (native_width &&
1311 (iter->t.hact != native_width ||
1312 iter->t.vact != native_height)) {
1313 native_mismatch = true;
1318 for (vec_timings_ext::iterator iter = cta.native_timing_nvrdb.begin();
1319 iter != cta.native_timing_nvrdb.end(); ++iter) {
1320 if (iter->t.interlaced) {
1321 fail("Interlaced native timing in NVRDB.\n");
1322 } else {
1323 native_nvrdb.insert(std::pair<unsigned, unsigned>(iter->t.hact, iter->t.vact));
1324 if (!native_width) {
1325 native_width = iter->t.hact;
1326 native_height = iter->t.vact;
1327 native_mismatch = true;
1328 } else if (native_width &&
1329 (iter->t.hact != native_width ||
1330 iter->t.vact != native_height)) {
1331 native_mismatch = true;
1336 if (diagonal) {
1337 if (image_width) {
1338 double w = image_width;
1339 double h = image_height;
1340 double d = sqrt(w * w + h * h) / 254.0;
1342 if (fabs(diagonal - d) >= 0.1)
1343 warn("Specified diagonal is %.1f\", calculated diagonal is %.1f\".\n",
1344 diagonal, d);
1346 if (native_width) {
1347 double w = native_width;
1348 double h = native_height;
1349 double d = diagonal * 254.0;
1350 double c = sqrt((d * d) / (w * w + h * h));
1352 w *= c;
1353 h *= c;
1355 if (image_width) {
1356 printf("\n----------------\n");
1357 printf("\nCalculated image size for a diagonal of %.1f\" is %.1fx%.1fmm.\n",
1358 diagonal, w / 10.0, h / 10.0);
1360 if (fabs((double)image_width - w) >= 100.0 ||
1361 fabs((double)image_height - h) >= 100.0)
1362 warn("Calculated image size is %.1fx%.1fmm, EDID image size is %.1fx%.1fmm.\n",
1363 w / 10.0, h / 10.0,
1364 image_width / 10.0, image_height / 10.0);
1365 } else {
1366 warn("No image size was specified, but it is calculated as %.1fx%.1fmm.\n",
1367 w / 10.0, h / 10.0);
1372 if (!options[OptNativeResolution])
1373 return;
1375 if (native_width == 0 && native_width_int == 0) {
1376 printf("\n----------------\n");
1377 printf("\nNo Native Video Resolution was defined.\n");
1378 return;
1381 if ((native_width || native_width_int) &&
1382 !native_mismatch && !native_int_mismatch) {
1383 printf("\n----------------\n");
1384 printf("\nNative Video Resolution%s:\n",
1385 native_width && native_width_int ? "s" : "");
1386 if (native_width)
1387 printf(" %ux%u\n", native_width, native_height);
1388 if (native_width_int)
1389 printf(" %ux%ui\n", native_width_int, native_height_int);
1390 return;
1393 if (base.preferred_timing.is_valid() && base.preferred_is_also_native) {
1394 printf("\n----------------\n");
1395 printf("\nNative Video Resolution if only Block 0 is parsed:\n");
1396 printf(" %ux%u%s\n",
1397 base.preferred_timing.t.hact, base.preferred_timing.t.vact,
1398 base.preferred_timing.t.interlaced ? "i" : "");
1401 if (!cta.native_timings.empty()) {
1402 printf("\n----------------\n");
1403 printf("\nNative Video Resolution%s if Block 0 and CTA-861 Blocks are parsed:\n",
1404 native_prog.size() + native_int.size() > 1 ? "s" : "");
1405 for (resolution_set::iterator iter = native_prog.begin();
1406 iter != native_prog.end(); ++iter)
1407 printf(" %ux%u\n", iter->first, iter->second);
1408 for (resolution_set::iterator iter = native_int.begin();
1409 iter != native_int.end(); ++iter)
1410 printf(" %ux%ui\n", iter->first, iter->second);
1413 if (!cta.native_timing_nvrdb.empty()) {
1414 printf("\n----------------\n");
1415 printf("\nNative Video Resolution if Block 0 and CTA-861 Blocks are parsed with NVRDB support:\n");
1416 for (resolution_set::iterator iter = native_nvrdb.begin();
1417 iter != native_nvrdb.end(); ++iter)
1418 printf(" %ux%u\n", iter->first, iter->second);
1421 if (dispid.native_width) {
1422 printf("\n----------------\n");
1423 printf("\nNative Video Resolution if the DisplayID Blocks are parsed:\n");
1424 printf(" %ux%u\n", dispid.native_width, dispid.native_height);
1428 int edid_state::parse_edid()
1430 hide_serial_numbers = options[OptHideSerialNumbers];
1431 replace_unique_ids = options[OptReplaceUniqueIDs];
1433 preparse_base_block(edid);
1434 if (replace_unique_ids)
1435 replace_checksum(edid, EDID_PAGE_SIZE);
1437 for (unsigned i = 1; i < num_blocks; i++)
1438 preparse_extension(edid + i * EDID_PAGE_SIZE);
1440 if (options[OptPhysicalAddress]) {
1441 printf("%x.%x.%x.%x\n",
1442 (cta.preparsed_phys_addr >> 12) & 0xf,
1443 (cta.preparsed_phys_addr >> 8) & 0xf,
1444 (cta.preparsed_phys_addr >> 4) & 0xf,
1445 cta.preparsed_phys_addr & 0xf);
1446 return 0;
1449 if (!options[OptSkipHexDump]) {
1450 printf("edid-decode (hex):\n\n");
1451 for (unsigned i = 0; i < num_blocks; i++) {
1452 hex_block("", edid + i * EDID_PAGE_SIZE, EDID_PAGE_SIZE, false);
1453 if (i == num_blocks - 1 && options[OptOnlyHexDump])
1454 return 0;
1455 printf("\n");
1457 printf("----------------\n\n");
1460 block = block_name(0x00);
1461 printf("Block %u, %s:\n", block_nr, block.c_str());
1462 parse_base_block(edid);
1464 for (unsigned i = 1; i < num_blocks; i++) {
1465 block_nr++;
1466 printf("\n----------------\n");
1467 parse_extension(edid + i * EDID_PAGE_SIZE);
1470 block = "";
1471 block_nr = EDID_MAX_BLOCKS;
1473 if (has_cta)
1474 cta_resolve_svrs();
1476 if (options[OptPreferredTimings])
1477 print_preferred_timings();
1479 print_native_res();
1481 if (!options[OptCheck] && !options[OptCheckInline])
1482 return 0;
1484 check_base_block(edid);
1485 if (has_cta)
1486 check_cta_blocks();
1487 if (has_dispid)
1488 check_displayid_blocks();
1490 printf("\n----------------\n");
1492 if (!options[OptSkipSHA] && strlen(STRING(SHA))) {
1493 printf("\nedid-decode SHA: %s %s\n", STRING(SHA), STRING(DATE));
1496 if (options[OptCheck]) {
1497 if (warnings)
1498 show_msgs(true);
1499 if (failures)
1500 show_msgs(false);
1502 printf("\nEDID conformity: %s\n", failures ? "FAIL" : "PASS");
1503 return failures ? -2 : 0;
1506 #ifndef __EMSCRIPTEN__
1508 static unsigned char crc_calc(const unsigned char *b)
1510 unsigned char sum = 0;
1511 unsigned i;
1513 for (i = 0; i < 127; i++)
1514 sum += b[i];
1515 return 256 - sum;
1518 static int crc_ok(const unsigned char *b)
1520 return crc_calc(b) == b[127];
1523 static void hexdumpedid(FILE *f, const unsigned char *edid, unsigned size)
1525 unsigned b, i, j;
1527 for (b = 0; b < size / 128; b++) {
1528 const unsigned char *buf = edid + 128 * b;
1530 if (b)
1531 fprintf(f, "\n");
1532 for (i = 0; i < 128; i += 0x10) {
1533 fprintf(f, "%02x", buf[i]);
1534 for (j = 1; j < 0x10; j++) {
1535 fprintf(f, " %02x", buf[i + j]);
1537 fprintf(f, "\n");
1539 if (!crc_ok(buf))
1540 fprintf(f, "Block %u has a checksum error (should be 0x%02x).\n",
1541 b, crc_calc(buf));
1545 static void carraydumpedid(FILE *f, const unsigned char *edid, unsigned size)
1547 unsigned b, i, j;
1549 fprintf(f, "const unsigned char edid[] = {\n");
1550 for (b = 0; b < size / 128; b++) {
1551 const unsigned char *buf = edid + 128 * b;
1553 if (b)
1554 fprintf(f, "\n");
1555 for (i = 0; i < 128; i += 8) {
1556 fprintf(f, "\t0x%02x,", buf[i]);
1557 for (j = 1; j < 8; j++) {
1558 fprintf(f, " 0x%02x,", buf[i + j]);
1560 fprintf(f, "\n");
1562 if (!crc_ok(buf))
1563 fprintf(f, "\t/* Block %u has a checksum error (should be 0x%02x). */\n",
1564 b, crc_calc(buf));
1566 fprintf(f, "};\n");
1569 // This format can be read by the QuantumData EDID editor
1570 static void xmldumpedid(FILE *f, const unsigned char *edid, unsigned size)
1572 fprintf(f, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
1573 fprintf(f, "<DATAOBJ>\n");
1574 fprintf(f, " <HEADER TYPE=\"DID\" VERSION=\"1.0\"/>\n");
1575 fprintf(f, " <DATA>\n");
1576 for (unsigned b = 0; b < size / 128; b++) {
1577 const unsigned char *buf = edid + 128 * b;
1579 fprintf(f, " <BLOCK%u>", b);
1580 for (unsigned i = 0; i < 128; i++)
1581 fprintf(f, "%02X", buf[i]);
1582 fprintf(f, "</BLOCK%u>\n", b);
1584 fprintf(f, " </DATA>\n");
1585 fprintf(f, "</DATAOBJ>\n");
1588 static int edid_to_file(const char *to_file, enum output_format out_fmt)
1590 FILE *out;
1592 if (!strcmp(to_file, "-")) {
1593 to_file = "stdout";
1594 out = stdout;
1595 } else if ((out = fopen(to_file, "w")) == NULL) {
1596 perror(to_file);
1597 return -1;
1599 if (out_fmt == OUT_FMT_DEFAULT)
1600 out_fmt = out == stdout ? OUT_FMT_HEX : OUT_FMT_RAW;
1602 switch (out_fmt) {
1603 default:
1604 case OUT_FMT_HEX:
1605 hexdumpedid(out, edid, state.edid_size);
1606 break;
1607 case OUT_FMT_RAW:
1608 fwrite(edid, state.edid_size, 1, out);
1609 break;
1610 case OUT_FMT_CARRAY:
1611 carraydumpedid(out, edid, state.edid_size);
1612 break;
1613 case OUT_FMT_XML:
1614 xmldumpedid(out, edid, state.edid_size);
1615 break;
1618 if (out != stdout)
1619 fclose(out);
1620 return 0;
1623 enum cvt_opts {
1624 CVT_WIDTH = 0,
1625 CVT_HEIGHT,
1626 CVT_FPS,
1627 CVT_INTERLACED,
1628 CVT_OVERSCAN,
1629 CVT_RB,
1630 CVT_ALT,
1631 CVT_RB_H_BLANK,
1632 CVT_RB_V_BLANK,
1633 CVT_EARLY_VSYNC,
1636 static int parse_cvt_subopt(char **subopt_str, double *value)
1638 int opt;
1639 char *opt_str;
1641 static const char * const subopt_list[] = {
1642 "w",
1643 "h",
1644 "fps",
1645 "interlaced",
1646 "overscan",
1647 "rb",
1648 "alt",
1649 "hblank",
1650 "vblank",
1651 "early-vsync",
1652 nullptr
1655 opt = getsubopt(subopt_str, (char* const*) subopt_list, &opt_str);
1657 if (opt == -1) {
1658 fprintf(stderr, "Invalid suboptions specified.\n");
1659 usage();
1660 std::exit(EXIT_FAILURE);
1662 if (opt_str == nullptr && opt != CVT_INTERLACED && opt != CVT_ALT &&
1663 opt != CVT_OVERSCAN && opt != CVT_EARLY_VSYNC) {
1664 fprintf(stderr, "No value given to suboption <%s>.\n",
1665 subopt_list[opt]);
1666 usage();
1667 std::exit(EXIT_FAILURE);
1670 if (opt_str)
1671 *value = strtod(opt_str, nullptr);
1672 return opt;
1675 static void parse_cvt(char *optarg)
1677 unsigned w = 0, h = 0;
1678 double fps = 0;
1679 unsigned rb = RB_NONE;
1680 unsigned rb_h_blank = 0;
1681 unsigned rb_v_blank = 460;
1682 bool interlaced = false;
1683 bool alt = false;
1684 bool overscan = false;
1685 bool early_vsync = false;
1687 while (*optarg != '\0') {
1688 int opt;
1689 double opt_val;
1691 opt = parse_cvt_subopt(&optarg, &opt_val);
1693 switch (opt) {
1694 case CVT_WIDTH:
1695 w = round(opt_val);
1696 break;
1697 case CVT_HEIGHT:
1698 h = round(opt_val);
1699 break;
1700 case CVT_FPS:
1701 fps = opt_val;
1702 break;
1703 case CVT_RB:
1704 rb = opt_val;
1705 break;
1706 case CVT_OVERSCAN:
1707 overscan = true;
1708 break;
1709 case CVT_INTERLACED:
1710 interlaced = opt_val;
1711 break;
1712 case CVT_ALT:
1713 alt = opt_val;
1714 break;
1715 case CVT_RB_H_BLANK:
1716 rb_h_blank = opt_val;
1717 break;
1718 case CVT_RB_V_BLANK:
1719 rb_v_blank = opt_val;
1720 if (rb_v_blank < 460) {
1721 fprintf(stderr, "vblank must be >= 460, set to 460.\n");
1722 rb_v_blank = 460;
1723 } else if (rb_v_blank > 705) {
1724 fprintf(stderr, "warning: vblank values > 705 might not be supported by RBv3 compliant sources.\n");
1726 break;
1727 case CVT_EARLY_VSYNC:
1728 early_vsync = true;
1729 break;
1730 default:
1731 break;
1735 if (!w || !h || !fps) {
1736 fprintf(stderr, "Missing width, height and/or fps.\n");
1737 usage();
1738 std::exit(EXIT_FAILURE);
1740 if (interlaced)
1741 fps /= 2;
1742 timings t = state.calc_cvt_mode(w, h, fps, rb, interlaced, overscan, alt,
1743 rb_h_blank, rb_v_blank, early_vsync);
1744 state.print_timings("", &t, "CVT", "", true, false);
1747 struct gtf_parsed_data {
1748 unsigned w, h;
1749 double freq;
1750 double C, M, K, J;
1751 bool overscan;
1752 bool interlaced;
1753 bool secondary;
1754 bool params_from_edid;
1755 enum gtf_ip_parm ip_parm;
1758 enum gtf_opts {
1759 GTF_WIDTH = 0,
1760 GTF_HEIGHT,
1761 GTF_FPS,
1762 GTF_HORFREQ,
1763 GTF_PIXCLK,
1764 GTF_INTERLACED,
1765 GTF_OVERSCAN,
1766 GTF_SECONDARY,
1767 GTF_C2,
1768 GTF_M,
1769 GTF_K,
1770 GTF_J2,
1773 static int parse_gtf_subopt(char **subopt_str, double *value)
1775 int opt;
1776 char *opt_str;
1778 static const char * const subopt_list[] = {
1779 "w",
1780 "h",
1781 "fps",
1782 "horfreq",
1783 "pixclk",
1784 "interlaced",
1785 "overscan",
1786 "secondary",
1787 "C",
1788 "M",
1789 "K",
1790 "J",
1791 nullptr
1794 opt = getsubopt(subopt_str, (char * const *)subopt_list, &opt_str);
1796 if (opt == -1) {
1797 fprintf(stderr, "Invalid suboptions specified.\n");
1798 usage();
1799 std::exit(EXIT_FAILURE);
1801 if (opt_str == nullptr && opt != GTF_INTERLACED && opt != GTF_OVERSCAN &&
1802 opt != GTF_SECONDARY) {
1803 fprintf(stderr, "No value given to suboption <%s>.\n",
1804 subopt_list[opt]);
1805 usage();
1806 std::exit(EXIT_FAILURE);
1809 if (opt == GTF_C2 || opt == GTF_J2)
1810 *value = round(2.0 * strtod(opt_str, nullptr));
1811 else if (opt_str)
1812 *value = strtod(opt_str, nullptr);
1813 return opt;
1816 static void parse_gtf(char *optarg, gtf_parsed_data &data)
1818 memset(&data, 0, sizeof(data));
1819 data.params_from_edid = true;
1820 data.C = 40;
1821 data.M = 600;
1822 data.K = 128;
1823 data.J = 20;
1825 while (*optarg != '\0') {
1826 int opt;
1827 double opt_val;
1829 opt = parse_gtf_subopt(&optarg, &opt_val);
1831 switch (opt) {
1832 case GTF_WIDTH:
1833 data.w = round(opt_val);
1834 break;
1835 case GTF_HEIGHT:
1836 data.h = round(opt_val);
1837 break;
1838 case GTF_FPS:
1839 data.freq = opt_val;
1840 data.ip_parm = gtf_ip_vert_freq;
1841 break;
1842 case GTF_HORFREQ:
1843 data.freq = opt_val;
1844 data.ip_parm = gtf_ip_hor_freq;
1845 break;
1846 case GTF_PIXCLK:
1847 data.freq = opt_val;
1848 data.ip_parm = gtf_ip_clk_freq;
1849 break;
1850 case GTF_INTERLACED:
1851 data.interlaced = true;
1852 break;
1853 case GTF_OVERSCAN:
1854 data.overscan = true;
1855 break;
1856 case GTF_SECONDARY:
1857 data.secondary = true;
1858 break;
1859 case GTF_C2:
1860 data.C = opt_val / 2.0;
1861 data.params_from_edid = false;
1862 break;
1863 case GTF_M:
1864 data.M = round(opt_val);
1865 data.params_from_edid = false;
1866 break;
1867 case GTF_K:
1868 data.K = round(opt_val);
1869 data.params_from_edid = false;
1870 break;
1871 case GTF_J2:
1872 data.J = opt_val / 2.0;
1873 data.params_from_edid = false;
1874 break;
1875 default:
1876 break;
1880 if (!data.w || !data.h) {
1881 fprintf(stderr, "Missing width and/or height.\n");
1882 usage();
1883 std::exit(EXIT_FAILURE);
1885 if (!data.freq) {
1886 fprintf(stderr, "One of fps, horfreq or pixclk must be given.\n");
1887 usage();
1888 std::exit(EXIT_FAILURE);
1890 if (!data.secondary)
1891 data.params_from_edid = false;
1892 if (data.interlaced && data.ip_parm == gtf_ip_vert_freq)
1893 data.freq /= 2;
1896 static void show_gtf(gtf_parsed_data &data)
1898 timings t;
1900 t = state.calc_gtf_mode(data.w, data.h, data.freq, data.interlaced,
1901 data.ip_parm, data.overscan, data.secondary,
1902 data.C, data.M, data.K, data.J);
1903 calc_ratio(&t);
1904 state.print_timings("", &t, "GTF", "", true, false);
1907 enum ovt_opts {
1908 OVT_RID,
1909 OVT_WIDTH,
1910 OVT_HEIGHT,
1911 OVT_FPS,
1914 static int parse_ovt_subopt(char **subopt_str, unsigned *value)
1916 int opt;
1917 char *opt_str;
1919 static const char * const subopt_list[] = {
1920 "rid",
1921 "w",
1922 "h",
1923 "fps",
1924 nullptr
1927 opt = getsubopt(subopt_str, (char* const*) subopt_list, &opt_str);
1929 if (opt == -1) {
1930 fprintf(stderr, "Invalid suboptions specified.\n");
1931 usage();
1932 std::exit(EXIT_FAILURE);
1934 if (opt_str == nullptr) {
1935 fprintf(stderr, "No value given to suboption <%s>.\n",
1936 subopt_list[opt]);
1937 usage();
1938 std::exit(EXIT_FAILURE);
1941 if (opt_str)
1942 *value = strtoul(opt_str, NULL, 0);
1943 return opt;
1946 static void parse_ovt(char *optarg)
1948 unsigned rid = 0;
1949 unsigned w = 0, h = 0;
1950 unsigned fps = 0;
1952 while (*optarg != '\0') {
1953 int opt;
1954 unsigned opt_val;
1956 opt = parse_ovt_subopt(&optarg, &opt_val);
1958 switch (opt) {
1959 case OVT_RID:
1960 rid = opt_val;
1961 break;
1962 case OVT_WIDTH:
1963 w = opt_val;
1964 break;
1965 case OVT_HEIGHT:
1966 h = opt_val;
1967 break;
1968 case OVT_FPS:
1969 fps = opt_val;
1970 break;
1971 default:
1972 break;
1976 if ((!rid && (!w || !h)) || !fps) {
1977 fprintf(stderr, "Missing rid, width, height and/or fps.\n");
1978 usage();
1979 std::exit(EXIT_FAILURE);
1981 unsigned hratio = 0, vratio = 0;
1982 if (rid) {
1983 const cta_rid *r = find_rid(rid);
1985 if (r) {
1986 w = r->hact;
1987 h = r->vact;
1988 hratio = r->hratio;
1989 vratio = r->vratio;
1992 timings t = state.calc_ovt_mode(w, h, hratio, vratio, fps);
1993 state.print_timings("", &t, "OVT", "", true, false);
1996 int main(int argc, char **argv)
1998 char short_options[26 * 2 * 2 + 1];
1999 enum output_format out_fmt = OUT_FMT_DEFAULT;
2000 gtf_parsed_data gtf_data;
2001 unsigned list_rid = 0;
2002 int ret;
2004 while (1) {
2005 int option_index = 0;
2006 unsigned idx = 0;
2007 unsigned i, val;
2008 const timings *t;
2009 char buf[16];
2011 for (i = 0; long_options[i].name; i++) {
2012 if (!isalpha(long_options[i].val))
2013 continue;
2014 short_options[idx++] = long_options[i].val;
2015 if (long_options[i].has_arg == required_argument)
2016 short_options[idx++] = ':';
2018 short_options[idx] = 0;
2019 int ch = getopt_long(argc, argv, short_options,
2020 long_options, &option_index);
2021 if (ch == -1)
2022 break;
2024 options[ch] = 1;
2025 switch (ch) {
2026 case OptHelp:
2027 usage();
2028 return -1;
2029 case OptOutputFormat:
2030 if (!strcmp(optarg, "hex")) {
2031 out_fmt = OUT_FMT_HEX;
2032 } else if (!strcmp(optarg, "raw")) {
2033 out_fmt = OUT_FMT_RAW;
2034 } else if (!strcmp(optarg, "carray")) {
2035 out_fmt = OUT_FMT_CARRAY;
2036 } else if (!strcmp(optarg, "xml")) {
2037 out_fmt = OUT_FMT_XML;
2038 } else {
2039 usage();
2040 exit(1);
2042 break;
2043 case OptDiag:
2044 state.diagonal = strtod(optarg, NULL);
2045 break;
2046 case OptSTD: {
2047 unsigned char byte1, byte2 = 0;
2048 char *endptr;
2050 byte1 = strtoul(optarg, &endptr, 0);
2051 if (*endptr == ',')
2052 byte2 = strtoul(endptr + 1, NULL, 0);
2053 state.print_standard_timing("", byte1, byte2, false, true);
2054 break;
2056 case OptDMT:
2057 val = strtoul(optarg, NULL, 0);
2058 t = find_dmt_id(val);
2059 if (t) {
2060 sprintf(buf, "DMT 0x%02x", val);
2061 state.print_timings("", t, buf, "", true, false);
2062 } else {
2063 fprintf(stderr, "Unknown DMT code 0x%02x.\n", val);
2065 break;
2066 case OptVIC:
2067 val = strtoul(optarg, NULL, 0);
2068 t = find_vic_id(val);
2069 if (t) {
2070 sprintf(buf, "VIC %3u", val);
2071 state.print_timings("", t, buf, "", true, false);
2072 } else {
2073 fprintf(stderr, "Unknown VIC code %u.\n", val);
2075 break;
2076 case OptHDMIVIC:
2077 val = strtoul(optarg, NULL, 0);
2078 t = find_hdmi_vic_id(val);
2079 if (t) {
2080 sprintf(buf, "HDMI VIC %u", val);
2081 state.print_timings("", t, buf, "", true, false);
2082 } else {
2083 fprintf(stderr, "Unknown HDMI VIC code %u.\n", val);
2085 break;
2086 case OptCVT:
2087 parse_cvt(optarg);
2088 break;
2089 case OptGTF:
2090 parse_gtf(optarg, gtf_data);
2091 break;
2092 case OptOVT:
2093 parse_ovt(optarg);
2094 break;
2095 case OptListRIDTimings:
2096 list_rid = strtoul(optarg, NULL, 0);
2097 break;
2098 case ':':
2099 fprintf(stderr, "Option '%s' requires a value.\n",
2100 argv[optind]);
2101 usage();
2102 return -1;
2103 case '?':
2104 fprintf(stderr, "Unknown argument '%s'.\n",
2105 argv[optind]);
2106 usage();
2107 return -1;
2110 if (optind == argc && options[OptVersion]) {
2111 if (strlen(STRING(SHA)))
2112 printf("edid-decode SHA: %s %s\n", STRING(SHA), STRING(DATE));
2113 else
2114 printf("edid-decode SHA: not available\n");
2115 return 0;
2118 if (options[OptListEstTimings])
2119 state.list_established_timings();
2120 if (options[OptListDMTs])
2121 state.list_dmts();
2122 if (options[OptListVICs])
2123 state.cta_list_vics();
2124 if (options[OptListHDMIVICs])
2125 state.cta_list_hdmi_vics();
2126 if (options[OptListRIDs])
2127 state.cta_list_rids();
2128 if (options[OptListRIDTimings])
2129 state.cta_list_rid_timings(list_rid);
2131 if (options[OptListEstTimings] || options[OptListDMTs] ||
2132 options[OptListVICs] || options[OptListHDMIVICs] ||
2133 options[OptListRIDs] || options[OptListRIDTimings])
2134 return 0;
2136 if (options[OptCVT] || options[OptDMT] || options[OptVIC] ||
2137 options[OptHDMIVIC] || options[OptSTD] || options[OptOVT])
2138 return 0;
2140 if (options[OptGTF] && (!gtf_data.params_from_edid || optind == argc)) {
2141 show_gtf(gtf_data);
2142 return 0;
2145 if (optind == argc)
2146 ret = edid_from_file("-", stdout);
2147 else
2148 ret = edid_from_file(argv[optind], argv[optind + 1] ? stderr : stdout);
2150 if (ret && options[OptPhysicalAddress]) {
2151 printf("f.f.f.f\n");
2152 return 0;
2154 if (optind < argc - 1)
2155 return ret ? ret : edid_to_file(argv[optind + 1], out_fmt);
2157 if (options[OptGTF]) {
2158 timings t;
2160 state.preparse_base_block(edid);
2162 t = state.calc_gtf_mode(gtf_data.w, gtf_data.h, gtf_data.freq,
2163 gtf_data.interlaced, gtf_data.ip_parm,
2164 gtf_data.overscan);
2165 unsigned hbl = t.hfp + t.hsync + t.hbp;
2166 unsigned htotal = t.hact + hbl;
2167 double hor_freq_khz = htotal ? (double)t.pixclk_khz / htotal : 0;
2169 if (state.base.supports_sec_gtf &&
2170 hor_freq_khz >= state.base.sec_gtf_start_freq) {
2171 t = state.calc_gtf_mode(gtf_data.w, gtf_data.h, gtf_data.freq,
2172 gtf_data.interlaced, gtf_data.ip_parm,
2173 gtf_data.overscan, true,
2174 state.base.C, state.base.M,
2175 state.base.K, state.base.J);
2177 calc_ratio(&t);
2178 if (t.hfp <= 0)
2179 state.print_timings("", &t, "GTF", "INVALID: Hfront <= 0", true, false);
2180 else
2181 state.print_timings("", &t, "GTF", "", true, false);
2182 return 0;
2185 return ret ? ret : state.parse_edid();
2188 #else
2191 * The surrounding JavaScript implementation will call this function
2192 * each time it wants to decode an EDID. So this should reset all the
2193 * state and start over.
2195 extern "C" int parse_edid(const char *input)
2197 for (unsigned i = 0; i < EDID_MAX_BLOCKS + 1; i++) {
2198 s_msgs[i][0].clear();
2199 s_msgs[i][1].clear();
2201 options[OptCheck] = 1;
2202 options[OptPreferredTimings] = 1;
2203 options[OptNativeResolution] = 1;
2204 state = edid_state();
2205 int ret = edid_from_file(input, stderr);
2206 return ret ? ret : state.parse_edid();
2209 #endif