edid-decode: report if a DTD matches an absent VIC
[edid-decode.git] / edid-decode.h
blob7ae67028613dce63bc2bb9acd65e0f234e249e7e
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 #ifndef __EDID_DECODE_H_
11 #define __EDID_DECODE_H_
13 #include <string>
14 #include <vector>
15 #include <set>
16 #include <string.h>
18 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
19 #define min(a, b) ((a) < (b) ? (a) : (b))
20 #define max(a, b) ((a) > (b) ? (a) : (b))
22 #define EDID_PAGE_SIZE 128U
23 #define EDID_MAX_BLOCKS 256U
25 #define RB_ALT (1U << 7)
27 #define RB_NONE (0U)
28 #define RB_CVT_V1 (1U)
29 #define RB_CVT_V2 (2U)
30 #define RB_CVT_V3 (3U)
31 #define RB_GTF (4U)
33 // Video Timings
34 // If interlaced is true, then the vertical blanking
35 // for each field is (vfp + vsync + vbp + 0.5), except for
36 // the VIC 39 timings that doesn't have the 0.5 constant.
38 // The sequence of the various video parameters is as follows:
40 // border - front porch - sync - back porch - border - active video
42 // Note: this is slightly different from EDID 1.4 which calls
43 // 'active video' as 'addressable video' and the EDID 1.4 term
44 // 'active video' includes the borders.
46 // But since borders are rarely used, the term 'active video' will
47 // typically be the same as 'addressable video', and that's how I
48 // use it.
49 struct timings {
50 // Active horizontal and vertical frame height, excluding any
51 // borders, if present.
52 // Note: for interlaced formats the active field height is vact / 2
53 unsigned hact, vact;
54 unsigned hratio, vratio;
55 unsigned pixclk_khz;
56 // 0: no reduced blanking
57 // 1: CVT reduced blanking version 1
58 // 2: CVT reduced blanking version 2
59 // 2 | RB_ALT: CVT reduced blanking version 2 video-optimized (1000/1001 fps)
60 // 3: CVT reduced blanking version 3
61 // 3 | RB_ALT: v3 with a horizontal blank of 160
62 // 4: GTF Secondary Curve
63 unsigned rb;
64 bool interlaced;
65 // The horizontal frontporch may be negative in GTF calculations,
66 // so use int instead of unsigned for hfp. Example: 292x176@76.
67 int hfp;
68 unsigned hsync;
69 // The backporch may be negative in buggy detailed timings.
70 // So use int instead of unsigned for hbp and vbp.
71 int hbp;
72 bool pos_pol_hsync;
73 // For interlaced formats the vertical front porch of the Even Field
74 // is actually a half-line longer.
75 unsigned vfp, vsync;
76 // For interlaced formats the vertical back porch of the Odd Field
77 // is actually a half-line longer.
78 int vbp;
79 bool pos_pol_vsync;
80 unsigned hborder, vborder;
81 bool even_vtotal; // special for VIC 39
82 bool no_pol_vsync; // digital composite signals have no vsync polarity
83 unsigned hsize_mm, vsize_mm;
84 bool ycbcr420; // YCbCr 4:2:0 encoding
87 struct timings_ext {
88 timings_ext()
90 memset(&t, 0, sizeof(t));
92 timings_ext(unsigned svr, const std::string &_type)
94 memset(&t, 0, sizeof(t));
95 t.hact = svr;
96 type = _type;
98 timings_ext(const timings &_t, const std::string &_type, const std::string &_flags)
100 t = _t;
101 type = _type;
102 flags = _flags;
105 bool is_valid() const { return t.hact; }
106 bool has_svr() const { return t.hact && !t.vact; }
107 unsigned svr() const { return t.hact; }
108 timings t;
109 std::string type;
110 std::string flags;
113 enum gtf_ip_parm {
114 gtf_ip_vert_freq = 1,
115 gtf_ip_hor_freq,
116 gtf_ip_clk_freq,
119 typedef std::vector<timings_ext> vec_timings_ext;
121 struct cta_rid {
122 unsigned hact, vact;
123 unsigned hratio, vratio;
126 struct cta_vfd {
127 unsigned char rid;
128 unsigned char fr_factor;
129 unsigned int bfr50:1;
130 unsigned int fr24:1;
131 unsigned int bfr60:1;
132 unsigned int fr144:1;
133 unsigned int fr48:1;
136 struct edid_state {
137 edid_state()
139 // Global state
140 edid_size = num_blocks = block_nr = 0;
141 max_hor_freq_hz = max_vert_freq_hz = max_pixclk_khz = 0;
142 min_hor_freq_hz = 0xffffff;
143 min_vert_freq_hz = 0xffffffff;
144 dtd_max_vsize_mm = dtd_max_hsize_mm = 0;
145 warnings = failures = 0;
146 has_cta = has_dispid = false;
147 hide_serial_numbers = false;
148 replace_unique_ids = false;
149 image_width = image_height = diagonal = 0;
151 // Base block state
152 base.edid_minor = 0;
153 base.has_name_descriptor = base.has_display_range_descriptor =
154 base.has_serial_number = base.has_serial_string =
155 base.supports_continuous_freq = base.supports_gtf =
156 base.supports_cvt = base.seen_non_detailed_descriptor =
157 base.has_640x480p60_est_timing = base.has_spwg =
158 base.preferred_is_also_native = false;
159 base.supports_sec_gtf = false;
160 base.sec_gtf_start_freq = 0;
161 base.C = base.M = base.K = base.J = 0;
162 base.max_pos_neg_hor_freq_khz = 0;
163 base.uses_srgb = false;
164 base.detailed_block_cnt = base.dtd_cnt = 0;
166 base.min_display_hor_freq_hz = base.max_display_hor_freq_hz =
167 base.min_display_vert_freq_hz = base.max_display_vert_freq_hz =
168 base.max_display_pixclk_khz = base.max_display_width_mm =
169 base.max_display_height_mm = 0;
171 // CTA-861 block state
172 cta.has_vic_1 = cta.first_svd_might_be_preferred = cta.has_sldb =
173 cta.has_hdmi = cta.has_vcdb = cta.has_vfpdb = cta.has_cdb =
174 cta.has_nvrdb = false;
175 cta.previous_cta_tag = 0xfff;
176 cta.have_hf_vsdb = cta.have_hf_scdb = false;
177 cta.hf_eeodb_blocks = 0;
178 cta.image_width = cta.image_height = 0;
179 cta.block_number = 0;
180 cta.first_svd = true;
181 cta.supported_hdmi_vic_codes = cta.supported_hdmi_vic_vsb_codes = 0;
182 memset(cta.vics, 0, sizeof(cta.vics));
183 memset(cta.preparsed_has_vic, 0, sizeof(cta.preparsed_has_vic));
184 memset(&cta.preparsed_first_vfd, 0, sizeof(cta.preparsed_first_vfd));
185 cta.preparsed_phys_addr = 0xffff;
186 cta.preparsed_speaker_count = 0;
187 cta.preparsed_sld = false;
188 cta.preparsed_sld_has_coord = false;
189 cta.preparsed_total_dtds = 0;
190 cta.preparsed_total_vtdbs = 0;
191 cta.preparsed_has_t8vtdb = false;
192 cta.preparsed_t8vtdb_dmt = 0;
194 // DisplayID block state
195 dispid.version = 0;
196 dispid.native_width = dispid.native_height = 0;
197 dispid.preparsed_color_ids = dispid.preparsed_xfer_ids = 0;
198 dispid.preparsed_displayid_blocks = 0;
199 dispid.is_base_block = true;
200 dispid.is_display = dispid.has_product_identification =
201 dispid.has_display_parameters = dispid.has_type_1_7 =
202 dispid.has_display_interface_features = false;
203 dispid.block_number = 0;
204 dispid.image_width = dispid.image_height = 0;
206 // Block Map block state
207 block_map.saw_block_1 = false;
208 block_map.saw_block_128 = false;
211 // Global state
212 unsigned edid_size;
213 unsigned num_blocks;
214 unsigned block_nr;
215 std::string block;
216 std::string data_block;
217 unsigned unused_bytes;
218 bool has_cta;
219 bool has_dispid;
220 bool hide_serial_numbers;
221 bool replace_unique_ids;
223 unsigned min_hor_freq_hz;
224 unsigned max_hor_freq_hz;
225 double min_vert_freq_hz;
226 double max_vert_freq_hz;
227 unsigned max_pixclk_khz;
228 unsigned dtd_max_hsize_mm;
229 unsigned dtd_max_vsize_mm;
231 // in 0.1 mm units
232 unsigned image_width, image_height;
233 // in inches
234 double diagonal;
236 unsigned warnings;
237 unsigned failures;
239 // Base block state
240 struct {
241 unsigned edid_minor;
242 bool has_name_descriptor;
243 bool has_display_range_descriptor;
244 bool has_serial_number;
245 bool has_serial_string;
246 bool supports_continuous_freq;
247 bool supports_gtf;
248 bool supports_sec_gtf;
249 unsigned sec_gtf_start_freq;
250 double C, M, K, J;
251 bool supports_cvt;
252 bool has_spwg;
253 bool uses_srgb;
254 unsigned detailed_block_cnt;
255 unsigned dtd_cnt;
256 bool seen_non_detailed_descriptor;
257 bool has_640x480p60_est_timing;
258 bool preferred_is_also_native;
259 timings_ext preferred_timing;
261 unsigned min_display_hor_freq_hz;
262 unsigned max_display_hor_freq_hz;
263 unsigned min_display_vert_freq_hz;
264 unsigned max_display_vert_freq_hz;
265 unsigned max_display_pixclk_khz;
266 unsigned max_display_width_mm;
267 unsigned max_display_height_mm;
268 unsigned max_pos_neg_hor_freq_khz;
269 } base;
271 // CTA-861 block state
272 struct {
273 unsigned preparsed_total_dtds;
274 vec_timings_ext vec_dtds;
275 unsigned preparsed_total_vtdbs;
276 vec_timings_ext vec_vtdbs;
277 cta_vfd preparsed_first_vfd;
278 vec_timings_ext preferred_timings;
279 vec_timings_ext preferred_timings_vfpdb;
280 bool preparsed_has_t8vtdb;
281 unsigned preparsed_t8vtdb_dmt;
282 // Keep track of the found Tag/Extended Tag pairs.
283 // The unsigned value is equal to: (tag) | (OUI enum << 12) or (extended tag) | (tag << 8) | (OUI enum << 12)
284 std::vector<unsigned> found_tags;
285 timings_ext t8vtdb;
286 vec_timings_ext native_timings;
287 vec_timings_ext native_timing_nvrdb;
288 // in 0.1 mm units
289 unsigned image_width, image_height;
290 bool has_vic_1;
291 bool first_svd_might_be_preferred;
292 unsigned char byte3;
293 bool has_hdmi;
294 bool has_vcdb;
295 bool has_vfpdb;
296 bool has_nvrdb;
297 bool has_cdb;
298 unsigned preparsed_speaker_count;
299 bool preparsed_sld_has_coord;
300 bool preparsed_sld;
301 bool has_sldb;
302 unsigned short preparsed_phys_addr;
303 unsigned previous_cta_tag;
304 bool have_hf_vsdb, have_hf_scdb;
305 unsigned hf_eeodb_blocks;
306 unsigned block_number;
307 bool first_svd;
308 unsigned supported_hdmi_vic_codes;
309 unsigned supported_hdmi_vic_vsb_codes;
310 unsigned short vics[256][2];
311 bool preparsed_has_vic[2][256];
312 std::vector<unsigned char> preparsed_svds[2];
313 } cta;
315 // DisplayID block state
316 struct {
317 unsigned char version;
318 unsigned short preparsed_color_ids;
319 unsigned short preparsed_xfer_ids;
320 unsigned preparsed_displayid_blocks;
321 bool is_base_block;
322 bool is_display;
323 bool has_product_identification;
324 bool has_display_parameters;
325 bool has_type_1_7;
326 bool has_display_interface_features;
327 vec_timings_ext preferred_timings;
328 unsigned native_width, native_height;
329 // in 0.1 mm units
330 unsigned image_width, image_height;
331 unsigned block_number;
332 // Keep track of the found CTA-861 Tag/Extended Tag pairs.
333 // The unsigned value is equal to: (tag) | (OUI enum << 12) or (extended tag) | (tag << 8) | (OUI enum << 12)
334 std::vector<unsigned> found_tags;
335 } dispid;
337 // Block Map block state
338 struct {
339 bool saw_block_1;
340 bool saw_block_128;
341 } block_map;
343 std::string dtd_type(unsigned dtd);
344 std::string dtd_type() { return dtd_type(base.dtd_cnt); }
345 bool print_timings(const char *prefix, const struct timings *t,
346 const char *type, const char *flags = "",
347 bool detailed = false, bool do_checks = true);
348 bool print_timings(const char *prefix, const struct timings_ext &t,
349 bool detailed = false, bool do_checks = true)
351 return print_timings(prefix, &t.t, t.type.c_str(), t.flags.c_str(),
352 detailed, do_checks);
354 timings calc_gtf_mode(unsigned h_pixels, unsigned v_lines,
355 double ip_freq_rqd, bool int_rqd = false,
356 enum gtf_ip_parm ip_parm = gtf_ip_vert_freq,
357 bool margins_rqd = false, bool secondary = false,
358 double C = 40, double M = 600, double K = 128, double J = 20);
359 void edid_gtf_mode(unsigned refresh, struct timings &t);
360 timings calc_cvt_mode(unsigned h_pixels, unsigned v_lines,
361 double ip_freq_rqd, unsigned rb, bool int_rqd = false,
362 bool margins_rqd = false, bool alt = false,
363 unsigned rb_h_blank = 0, unsigned rb_v_blank = 460,
364 bool early_vsync_rqd = false);
365 void edid_cvt_mode(unsigned refresh, struct timings &t, unsigned rb_h_blank = 0,
366 unsigned rb_v_blank = 460, bool early_vsync_rqd = false);
367 void detailed_cvt_descriptor(const char *prefix, const unsigned char *x, bool first);
368 timings calc_ovt_mode(unsigned hact, unsigned vact,
369 unsigned hratio, unsigned vratio,
370 unsigned frame_rate);
371 void print_standard_timing(const char *prefix, unsigned char b1, unsigned char b2,
372 bool gtf_only = false, bool show_both = false);
373 void detailed_display_range_limits(const unsigned char *x);
374 void detailed_epi(const unsigned char *x);
375 void detailed_timings(const char *prefix, const unsigned char *x,
376 bool base_or_cta = true);
377 bool preparse_detailed_block(unsigned char *x);
378 void preparse_base_block(unsigned char *x);
379 void detailed_block(const unsigned char *x);
380 void parse_base_block(const unsigned char *x);
381 void check_base_block(const unsigned char *x);
382 void list_dmts();
383 void list_established_timings();
385 void data_block_oui(std::string block_name, const unsigned char *x, unsigned length, unsigned *ouinum,
386 bool ignorezeros = false, bool do_ascii = false, bool big_endian = false);
388 void print_vic_index(const char *prefix, unsigned idx, const char *suffix, bool ycbcr420 = false);
389 void hdmi_latency(unsigned char vid_lat, unsigned char aud_lat, bool is_ilaced);
390 void cta_vcdb(const unsigned char *x, unsigned length);
391 void cta_svd(const unsigned char *x, unsigned n, bool for_ycbcr420);
392 void cta_vfdb(const unsigned char *x, unsigned n);
393 void cta_y420cmdb(const unsigned char *x, unsigned length);
394 void cta_print_svr(unsigned char svr, vec_timings_ext &vec_tim);
395 void cta_vfpdb(const unsigned char *x, unsigned length);
396 void cta_nvrdb(const unsigned char *x, unsigned length);
397 cta_vfd cta_parse_vfd(const unsigned char *x, unsigned lvfd);
398 void cta_rcdb(const unsigned char *x, unsigned length);
399 void cta_sldb(const unsigned char *x, unsigned length);
400 void cta_preparse_sldb(const unsigned char *x, unsigned length);
401 void cta_colorimetry_block(const unsigned char *x, unsigned length);
402 void cta_hdmi_block(const unsigned char *x, unsigned length);
403 void cta_displayid_type_7(const unsigned char *x, unsigned length);
404 void cta_displayid_type_8(const unsigned char *x, unsigned length);
405 void cta_displayid_type_10(const unsigned char *x, unsigned length);
406 void cta_block(const unsigned char *x, std::vector<unsigned> &found_tags);
407 void preparse_cta_block(unsigned char *x);
408 void parse_cta_block(const unsigned char *x);
409 void cta_resolve_svr(timings_ext &t_ext);
410 void cta_resolve_svrs();
411 void check_cta_blocks();
412 void cta_list_vics();
413 void cta_list_hdmi_vics();
414 void cta_list_rids();
415 void cta_list_rid_timings(unsigned list_rid = 0);
417 void set_displayid_native_res(unsigned w, unsigned h);
418 void parse_digital_interface(const unsigned char *x);
419 void parse_display_device(const unsigned char *x);
420 void parse_display_caps(const unsigned char *x);
421 void parse_display_xfer(const unsigned char *x);
422 void parse_di_ext_block(const unsigned char *x);
424 void check_displayid_datablock_revision(unsigned char hdr,
425 unsigned char valid_flags = 0,
426 unsigned char rev = 0);
427 void parse_displayid_product_id(const unsigned char *x);
428 std::string product_type(unsigned char x, bool heading);
429 void parse_displayid_interface_features(const unsigned char *x);
430 void parse_displayid_parameters(const unsigned char *x);
431 void parse_displayid_parameters_v2(const unsigned char *x, unsigned block_rev);
432 void parse_displayid_display_intf(const unsigned char *x);
433 void parse_displayid_color_characteristics(const unsigned char *x);
434 void parse_displayid_transfer_characteristics(const unsigned char *x);
435 void parse_displayid_stereo_display_intf(const unsigned char *x);
436 void parse_displayid_type_1_7_timing(const unsigned char *x,
437 bool type7, unsigned block_rev, bool is_cta = false);
438 void parse_displayid_type_2_timing(const unsigned char *x);
439 void parse_displayid_type_3_timing(const unsigned char *x);
440 void parse_displayid_type_4_8_timing(unsigned char type, unsigned short id, bool is_cta = false);
441 void parse_displayid_video_timing_range_limits(const unsigned char *x);
442 void parse_displayid_string(const unsigned char *x);
443 void parse_displayid_display_device(const unsigned char *x);
444 void parse_displayid_intf_power_sequencing(const unsigned char *x);
445 void parse_displayid_type_5_timing(const unsigned char *x);
446 void parse_displayid_tiled_display_topology(const unsigned char *x, bool is_v2);
447 void parse_displayid_type_6_timing(const unsigned char *x);
448 void parse_displayid_type_9_timing(const unsigned char *x);
449 void parse_displayid_dynamic_video_timings_range_limits(const unsigned char *x);
450 void parse_displayid_ContainerID(const unsigned char *x);
451 void parse_displayid_adaptive_sync(const unsigned char *x);
452 void parse_displayid_type_10_timing(const unsigned char *x, unsigned sz,
453 bool is_cta = false);
454 void preparse_displayid_block(unsigned char *x);
455 unsigned displayid_block(const unsigned version, const unsigned char *x, unsigned length);
456 void parse_displayid_block(const unsigned char *x);
457 void parse_displayid_vesa(const unsigned char *x);
458 void parse_displayid_apple(const unsigned char *x);
459 void parse_displayid_cta_data_block(const unsigned char *x);
460 void check_displayid_blocks();
462 void parse_vtb_ext_block(const unsigned char *x);
464 void parse_string_table(const unsigned char *x);
465 void preparse_ls_ext_block(unsigned char *x);
466 void parse_ls_ext_block(const unsigned char *x);
468 void parse_block_map(const unsigned char *x);
470 void preparse_extension(unsigned char *x);
471 void parse_extension(const unsigned char *x);
472 void print_preferred_timings();
473 void print_native_res();
474 int parse_edid();
477 static inline void add_str(std::string &s, const std::string &add)
479 if (s.empty())
480 s = add;
481 else if (!add.empty())
482 s = s + ", " + add;
485 void msg(bool is_warn, const char *fmt, ...);
487 #ifdef _WIN32
489 #define warn(fmt, ...) msg(true, fmt, __VA_ARGS__)
490 #define warn_once(fmt, ...) \
491 do { \
492 static bool shown_warn; \
493 if (!shown_warn) { \
494 shown_warn = true; \
495 msg(true, fmt, __VA_ARGS__); \
497 } while (0)
498 #define fail(fmt, ...) msg(false, fmt, __VA_ARGS__)
500 #else
502 #define warn(fmt, args...) msg(true, fmt, ##args)
503 #define warn_once(fmt, args...) \
504 do { \
505 static bool shown_warn; \
506 if (!shown_warn) { \
507 shown_warn = true; \
508 msg(true, fmt, ##args); \
510 } while (0)
511 #define fail(fmt, args...) msg(false, fmt, ##args)
513 #endif
515 std::string utohex(unsigned char x);
516 std::string ouitohex(unsigned oui);
517 std::string containerid2s(const unsigned char *x);
518 bool memchk(const unsigned char *x, unsigned len, unsigned char v = 0);
519 void hex_block(const char *prefix, const unsigned char *x, unsigned length,
520 bool show_ascii = true, unsigned step = 16);
521 std::string block_name(unsigned char block);
522 void do_checksum(const char *prefix, const unsigned char *x, size_t len, unsigned unused_bytes = 0);
523 void replace_checksum(unsigned char *x, size_t len);
524 void calc_ratio(struct timings *t);
525 const char *oui_name(unsigned oui, unsigned *ouinum = NULL);
526 unsigned gcd(unsigned a, unsigned b);
528 bool match_timings(const timings &t1, const timings &t2);
529 bool timings_close_match(const timings &t1, const timings &t2);
530 const struct timings *find_dmt_id(unsigned char dmt_id);
531 const struct timings *close_match_to_dmt(const timings &t, unsigned &dmt);
532 const struct timings *find_vic_id(unsigned char vic);
533 const struct cta_rid *find_rid(unsigned char rid);
534 const struct timings *find_hdmi_vic_id(unsigned char hdmi_vic);
535 const struct timings *cta_close_match_to_vic(const timings &t, unsigned &vic);
536 bool cta_matches_vic(const timings &t, unsigned &vic);
537 unsigned char hdmi_vic_to_vic(unsigned char hdmi_vic);
538 char *extract_string(const unsigned char *x, unsigned len);
540 #define oneoui(c,k,n) const unsigned kOUI_##k = __LINE__<<12;
541 #include "oui.h"
543 #endif