edid-decode: update to CTA-861-I
[edid-decode.git] / edid-decode.h
blob9eb1e0af0c3a2fa707a51579f4bf3166f90c6fab
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 bool match_timings(const timings &t1, const timings &t2);
355 timings calc_gtf_mode(unsigned h_pixels, unsigned v_lines,
356 double ip_freq_rqd, bool int_rqd = false,
357 enum gtf_ip_parm ip_parm = gtf_ip_vert_freq,
358 bool margins_rqd = false, bool secondary = false,
359 double C = 40, double M = 600, double K = 128, double J = 20);
360 void edid_gtf_mode(unsigned refresh, struct timings &t);
361 timings calc_cvt_mode(unsigned h_pixels, unsigned v_lines,
362 double ip_freq_rqd, unsigned rb, bool int_rqd = false,
363 bool margins_rqd = false, bool alt = false,
364 unsigned rb_h_blank = 0, unsigned rb_v_blank = 460,
365 bool early_vsync_rqd = false);
366 void edid_cvt_mode(unsigned refresh, struct timings &t, unsigned rb_h_blank = 0,
367 unsigned rb_v_blank = 460, bool early_vsync_rqd = false);
368 void detailed_cvt_descriptor(const char *prefix, const unsigned char *x, bool first);
369 timings calc_ovt_mode(unsigned hact, unsigned vact,
370 unsigned hratio, unsigned vratio,
371 unsigned frame_rate);
372 void print_standard_timing(const char *prefix, unsigned char b1, unsigned char b2,
373 bool gtf_only = false, bool show_both = false);
374 void detailed_display_range_limits(const unsigned char *x);
375 void detailed_epi(const unsigned char *x);
376 void detailed_timings(const char *prefix, const unsigned char *x,
377 bool base_or_cta = true);
378 bool preparse_detailed_block(unsigned char *x);
379 void preparse_base_block(unsigned char *x);
380 void detailed_block(const unsigned char *x);
381 void parse_base_block(const unsigned char *x);
382 void check_base_block(const unsigned char *x);
383 void list_dmts();
384 void list_established_timings();
386 void data_block_oui(std::string block_name, const unsigned char *x, unsigned length, unsigned *ouinum,
387 bool ignorezeros = false, bool do_ascii = false, bool big_endian = false);
389 void print_vic_index(const char *prefix, unsigned idx, const char *suffix, bool ycbcr420 = false);
390 void hdmi_latency(unsigned char vid_lat, unsigned char aud_lat, bool is_ilaced);
391 void cta_vcdb(const unsigned char *x, unsigned length);
392 void cta_svd(const unsigned char *x, unsigned n, bool for_ycbcr420);
393 void cta_vfdb(const unsigned char *x, unsigned n);
394 void cta_y420cmdb(const unsigned char *x, unsigned length);
395 void cta_print_svr(unsigned char svr, vec_timings_ext &vec_tim);
396 void cta_vfpdb(const unsigned char *x, unsigned length);
397 void cta_nvrdb(const unsigned char *x, unsigned length);
398 cta_vfd cta_parse_vfd(const unsigned char *x, unsigned lvfd);
399 void cta_rcdb(const unsigned char *x, unsigned length);
400 void cta_sldb(const unsigned char *x, unsigned length);
401 void cta_preparse_sldb(const unsigned char *x, unsigned length);
402 void cta_colorimetry_block(const unsigned char *x, unsigned length);
403 void cta_hdmi_block(const unsigned char *x, unsigned length);
404 void cta_displayid_type_7(const unsigned char *x, unsigned length);
405 void cta_displayid_type_8(const unsigned char *x, unsigned length);
406 void cta_displayid_type_10(const unsigned char *x, unsigned length);
407 void cta_block(const unsigned char *x, std::vector<unsigned> &found_tags);
408 void preparse_cta_block(unsigned char *x);
409 void parse_cta_block(const unsigned char *x);
410 void cta_resolve_svr(timings_ext &t_ext);
411 void cta_resolve_svrs();
412 void check_cta_blocks();
413 void cta_list_vics();
414 void cta_list_hdmi_vics();
415 void cta_list_rids();
416 void cta_list_rid_timings(unsigned list_rid = 0);
418 void set_displayid_native_res(unsigned w, unsigned h);
419 void parse_digital_interface(const unsigned char *x);
420 void parse_display_device(const unsigned char *x);
421 void parse_display_caps(const unsigned char *x);
422 void parse_display_xfer(const unsigned char *x);
423 void parse_di_ext_block(const unsigned char *x);
425 void check_displayid_datablock_revision(unsigned char hdr,
426 unsigned char valid_flags = 0,
427 unsigned char rev = 0);
428 void parse_displayid_product_id(const unsigned char *x);
429 std::string product_type(unsigned char x, bool heading);
430 void parse_displayid_interface_features(const unsigned char *x);
431 void parse_displayid_parameters(const unsigned char *x);
432 void parse_displayid_parameters_v2(const unsigned char *x, unsigned block_rev);
433 void parse_displayid_display_intf(const unsigned char *x);
434 void parse_displayid_color_characteristics(const unsigned char *x);
435 void parse_displayid_transfer_characteristics(const unsigned char *x);
436 void parse_displayid_stereo_display_intf(const unsigned char *x);
437 void parse_displayid_type_1_7_timing(const unsigned char *x,
438 bool type7, unsigned block_rev, bool is_cta = false);
439 void parse_displayid_type_2_timing(const unsigned char *x);
440 void parse_displayid_type_3_timing(const unsigned char *x);
441 void parse_displayid_type_4_8_timing(unsigned char type, unsigned short id, bool is_cta = false);
442 void parse_displayid_video_timing_range_limits(const unsigned char *x);
443 void parse_displayid_string(const unsigned char *x);
444 void parse_displayid_display_device(const unsigned char *x);
445 void parse_displayid_intf_power_sequencing(const unsigned char *x);
446 void parse_displayid_type_5_timing(const unsigned char *x);
447 void parse_displayid_tiled_display_topology(const unsigned char *x, bool is_v2);
448 void parse_displayid_type_6_timing(const unsigned char *x);
449 void parse_displayid_type_9_timing(const unsigned char *x);
450 void parse_displayid_dynamic_video_timings_range_limits(const unsigned char *x);
451 void parse_displayid_ContainerID(const unsigned char *x);
452 void parse_displayid_adaptive_sync(const unsigned char *x);
453 void parse_displayid_type_10_timing(const unsigned char *x, unsigned sz,
454 bool is_cta = false);
455 void preparse_displayid_block(unsigned char *x);
456 unsigned displayid_block(const unsigned version, const unsigned char *x, unsigned length);
457 void parse_displayid_block(const unsigned char *x);
458 void parse_displayid_vesa(const unsigned char *x);
459 void parse_displayid_apple(const unsigned char *x);
460 void parse_displayid_cta_data_block(const unsigned char *x);
461 void check_displayid_blocks();
463 void parse_vtb_ext_block(const unsigned char *x);
465 void parse_string_table(const unsigned char *x);
466 void preparse_ls_ext_block(unsigned char *x);
467 void parse_ls_ext_block(const unsigned char *x);
469 void parse_block_map(const unsigned char *x);
471 void preparse_extension(unsigned char *x);
472 void parse_extension(const unsigned char *x);
473 void print_preferred_timings();
474 void print_native_res();
475 int parse_edid();
478 static inline void add_str(std::string &s, const std::string &add)
480 if (s.empty())
481 s = add;
482 else if (!add.empty())
483 s = s + ", " + add;
486 void msg(bool is_warn, const char *fmt, ...);
488 #ifdef _WIN32
490 #define warn(fmt, ...) msg(true, fmt, __VA_ARGS__)
491 #define warn_once(fmt, ...) \
492 do { \
493 static bool shown_warn; \
494 if (!shown_warn) { \
495 shown_warn = true; \
496 msg(true, fmt, __VA_ARGS__); \
498 } while (0)
499 #define fail(fmt, ...) msg(false, fmt, __VA_ARGS__)
501 #else
503 #define warn(fmt, args...) msg(true, fmt, ##args)
504 #define warn_once(fmt, args...) \
505 do { \
506 static bool shown_warn; \
507 if (!shown_warn) { \
508 shown_warn = true; \
509 msg(true, fmt, ##args); \
511 } while (0)
512 #define fail(fmt, args...) msg(false, fmt, ##args)
514 #endif
516 std::string utohex(unsigned char x);
517 std::string ouitohex(unsigned oui);
518 std::string containerid2s(const unsigned char *x);
519 bool memchk(const unsigned char *x, unsigned len, unsigned char v = 0);
520 void hex_block(const char *prefix, const unsigned char *x, unsigned length,
521 bool show_ascii = true, unsigned step = 16);
522 std::string block_name(unsigned char block);
523 void do_checksum(const char *prefix, const unsigned char *x, size_t len, unsigned unused_bytes = 0);
524 void replace_checksum(unsigned char *x, size_t len);
525 void calc_ratio(struct timings *t);
526 const char *oui_name(unsigned oui, unsigned *ouinum = NULL);
527 unsigned gcd(unsigned a, unsigned b);
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 unsigned char hdmi_vic_to_vic(unsigned char hdmi_vic);
537 char *extract_string(const unsigned char *x, unsigned len);
539 #define oneoui(c,k,n) const unsigned kOUI_##k = __LINE__<<12;
540 #include "oui.h"
542 #endif