edid-decode: calculate and show remaining unused space
[edid-decode.git] / edid-decode.h
blobceb39b522fef4202d9bf27df465005c58679953e
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 image_width = image_height = diagonal = 0;
150 // Base block state
151 base.edid_minor = 0;
152 base.has_name_descriptor = base.has_display_range_descriptor =
153 base.has_serial_number = base.has_serial_string =
154 base.supports_continuous_freq = base.supports_gtf =
155 base.supports_cvt = base.seen_non_detailed_descriptor =
156 base.has_640x480p60_est_timing = base.has_spwg =
157 base.preferred_is_also_native = false;
158 base.supports_sec_gtf = false;
159 base.sec_gtf_start_freq = 0;
160 base.C = base.M = base.K = base.J = 0;
161 base.max_pos_neg_hor_freq_khz = 0;
162 base.detailed_block_cnt = base.dtd_cnt = 0;
164 base.min_display_hor_freq_hz = base.max_display_hor_freq_hz =
165 base.min_display_vert_freq_hz = base.max_display_vert_freq_hz =
166 base.max_display_pixclk_khz = base.max_display_width_mm =
167 base.max_display_height_mm = 0;
169 // CTA-861 block state
170 cta.has_vic_1 = cta.first_svd_might_be_preferred = cta.has_sldb =
171 cta.has_hdmi = cta.has_vcdb = cta.has_vfpdb = false;
172 cta.previous_cta_tag = 0xfff;
173 cta.have_hf_vsdb = cta.have_hf_scdb = false;
174 cta.image_width = cta.image_height = 0;
175 cta.block_number = 0;
176 cta.first_svd = true;
177 cta.supported_hdmi_vic_codes = cta.supported_hdmi_vic_vsb_codes = 0;
178 memset(cta.vics, 0, sizeof(cta.vics));
179 memset(cta.preparsed_has_vic, 0, sizeof(cta.preparsed_has_vic));
180 memset(&cta.preparsed_first_vfd, 0, sizeof(cta.preparsed_first_vfd));
181 cta.preparsed_phys_addr = 0xffff;
182 cta.preparsed_speaker_count = 0;
183 cta.preparsed_sld = false;
184 cta.preparsed_sld_has_coord = false;
185 cta.preparsed_total_dtds = 0;
186 cta.preparsed_total_vtdbs = 0;
187 cta.preparsed_has_t8vtdb = false;
189 // DisplayID block state
190 dispid.version = 0;
191 dispid.native_width = dispid.native_height = 0;
192 dispid.preparsed_color_ids = dispid.preparsed_xfer_ids = 0;
193 dispid.preparsed_displayid_blocks = 0;
194 dispid.is_base_block = true;
195 dispid.is_display = dispid.has_product_identification =
196 dispid.has_display_parameters = dispid.has_type_1_7 =
197 dispid.has_display_interface_features = false;
198 dispid.block_number = 0;
199 dispid.image_width = dispid.image_height = 0;
201 // Block Map block state
202 block_map.saw_block_1 = false;
203 block_map.saw_block_128 = false;
206 // Global state
207 unsigned edid_size;
208 unsigned num_blocks;
209 unsigned block_nr;
210 std::string block;
211 std::string data_block;
212 unsigned unused_bytes;
213 bool has_cta;
214 bool has_dispid;
215 bool hide_serial_numbers;
217 unsigned min_hor_freq_hz;
218 unsigned max_hor_freq_hz;
219 double min_vert_freq_hz;
220 double max_vert_freq_hz;
221 unsigned max_pixclk_khz;
222 unsigned dtd_max_hsize_mm;
223 unsigned dtd_max_vsize_mm;
225 // in 0.1 mm units
226 unsigned image_width, image_height;
227 // in inches
228 double diagonal;
230 unsigned warnings;
231 unsigned failures;
233 // Base block state
234 struct {
235 unsigned edid_minor;
236 bool has_name_descriptor;
237 bool has_display_range_descriptor;
238 bool has_serial_number;
239 bool has_serial_string;
240 bool supports_continuous_freq;
241 bool supports_gtf;
242 bool supports_sec_gtf;
243 unsigned sec_gtf_start_freq;
244 double C, M, K, J;
245 bool supports_cvt;
246 bool has_spwg;
247 unsigned detailed_block_cnt;
248 unsigned dtd_cnt;
249 bool seen_non_detailed_descriptor;
250 bool has_640x480p60_est_timing;
251 bool preferred_is_also_native;
252 timings_ext preferred_timing;
254 unsigned min_display_hor_freq_hz;
255 unsigned max_display_hor_freq_hz;
256 unsigned min_display_vert_freq_hz;
257 unsigned max_display_vert_freq_hz;
258 unsigned max_display_pixclk_khz;
259 unsigned max_display_width_mm;
260 unsigned max_display_height_mm;
261 unsigned max_pos_neg_hor_freq_khz;
262 } base;
264 // CTA-861 block state
265 struct {
266 unsigned preparsed_total_dtds;
267 vec_timings_ext vec_dtds;
268 unsigned preparsed_total_vtdbs;
269 vec_timings_ext vec_vtdbs;
270 cta_vfd preparsed_first_vfd;
271 vec_timings_ext preferred_timings;
272 bool preparsed_has_t8vtdb;
273 // Keep track of the found Tag/Extended Tag pairs.
274 // The unsigned value is equal to: (tag) | (OUI enum << 12) or (extended tag) | (tag << 8) | (OUI enum << 12)
275 std::vector<unsigned> found_tags;
276 timings_ext t8vtdb;
277 vec_timings_ext native_timings;
278 // in 0.1 mm units
279 unsigned image_width, image_height;
280 bool has_vic_1;
281 bool first_svd_might_be_preferred;
282 unsigned char byte3;
283 bool has_hdmi;
284 bool has_vcdb;
285 bool has_vfpdb;
286 unsigned preparsed_speaker_count;
287 bool preparsed_sld_has_coord;
288 bool preparsed_sld;
289 bool has_sldb;
290 unsigned short preparsed_phys_addr;
291 unsigned previous_cta_tag;
292 bool have_hf_vsdb, have_hf_scdb;
293 unsigned block_number;
294 bool first_svd;
295 unsigned supported_hdmi_vic_codes;
296 unsigned supported_hdmi_vic_vsb_codes;
297 unsigned short vics[256][2];
298 bool preparsed_has_vic[2][256];
299 std::vector<unsigned char> preparsed_svds[2];
300 } cta;
302 // DisplayID block state
303 struct {
304 unsigned char version;
305 unsigned short preparsed_color_ids;
306 unsigned short preparsed_xfer_ids;
307 unsigned preparsed_displayid_blocks;
308 bool is_base_block;
309 bool is_display;
310 bool has_product_identification;
311 bool has_display_parameters;
312 bool has_type_1_7;
313 bool has_display_interface_features;
314 vec_timings_ext preferred_timings;
315 unsigned native_width, native_height;
316 // in 0.1 mm units
317 unsigned image_width, image_height;
318 unsigned block_number;
319 // Keep track of the found CTA-861 Tag/Extended Tag pairs.
320 // The unsigned value is equal to: (tag) | (OUI enum << 12) or (extended tag) | (tag << 8) | (OUI enum << 12)
321 std::vector<unsigned> found_tags;
322 } dispid;
324 // Block Map block state
325 struct {
326 bool saw_block_1;
327 bool saw_block_128;
328 } block_map;
330 std::string dtd_type(unsigned dtd);
331 std::string dtd_type() { return dtd_type(base.dtd_cnt); }
332 bool print_timings(const char *prefix, const struct timings *t,
333 const char *type, const char *flags = "",
334 bool detailed = false, bool do_checks = true);
335 bool print_timings(const char *prefix, const struct timings_ext &t,
336 bool detailed = false, bool do_checks = true)
338 return print_timings(prefix, &t.t, t.type.c_str(), t.flags.c_str(),
339 detailed, do_checks);
341 bool match_timings(const timings &t1, const timings &t2);
342 timings calc_gtf_mode(unsigned h_pixels, unsigned v_lines,
343 double ip_freq_rqd, bool int_rqd = false,
344 enum gtf_ip_parm ip_parm = gtf_ip_vert_freq,
345 bool margins_rqd = false, bool secondary = false,
346 double C = 40, double M = 600, double K = 128, double J = 20);
347 void edid_gtf_mode(unsigned refresh, struct timings &t);
348 timings calc_cvt_mode(unsigned h_pixels, unsigned v_lines,
349 double ip_freq_rqd, unsigned rb, bool int_rqd = false,
350 bool margins_rqd = false, bool alt = false,
351 unsigned rb_h_blank = 0, unsigned rb_v_blank = 460,
352 bool early_vsync_rqd = false);
353 void edid_cvt_mode(unsigned refresh, struct timings &t, unsigned rb_h_blank = 0,
354 unsigned rb_v_blank = 460, bool early_vsync_rqd = false);
355 void detailed_cvt_descriptor(const char *prefix, const unsigned char *x, bool first);
356 timings calc_ovt_mode(unsigned hact, unsigned vact,
357 unsigned hratio, unsigned vratio,
358 unsigned frame_rate);
359 void print_standard_timing(const char *prefix, unsigned char b1, unsigned char b2,
360 bool gtf_only = false, bool show_both = false);
361 void detailed_display_range_limits(const unsigned char *x);
362 void detailed_epi(const unsigned char *x);
363 void detailed_timings(const char *prefix, const unsigned char *x,
364 bool base_or_cta = true);
365 void preparse_detailed_block(const unsigned char *x);
366 void detailed_block(const unsigned char *x);
367 void parse_base_block(const unsigned char *x);
368 void check_base_block();
369 void list_dmts();
370 void list_established_timings();
372 void data_block_oui(std::string block_name, const unsigned char *x, unsigned length, unsigned *ouinum,
373 bool ignorezeros = false, bool do_ascii = false, bool big_endian = false);
375 void print_vic_index(const char *prefix, unsigned idx, const char *suffix, bool ycbcr420 = false);
376 void hdmi_latency(unsigned char vid_lat, unsigned char aud_lat, bool is_ilaced);
377 void cta_vcdb(const unsigned char *x, unsigned length);
378 void cta_svd(const unsigned char *x, unsigned n, bool for_ycbcr420);
379 void cta_vfdb(const unsigned char *x, unsigned n);
380 void cta_y420cmdb(const unsigned char *x, unsigned length);
381 void cta_print_svr(unsigned char svr, vec_timings_ext &vec_tim);
382 void cta_vfpdb(const unsigned char *x, unsigned length);
383 void cta_nvrdb(const unsigned char *x, unsigned length);
384 cta_vfd cta_parse_vfd(const unsigned char *x, unsigned lvfd);
385 void cta_rcdb(const unsigned char *x, unsigned length);
386 void cta_sldb(const unsigned char *x, unsigned length);
387 void cta_preparse_sldb(const unsigned char *x, unsigned length);
388 void cta_hdmi_block(const unsigned char *x, unsigned length);
389 void cta_displayid_type_7(const unsigned char *x, unsigned length);
390 void cta_displayid_type_8(const unsigned char *x, unsigned length);
391 void cta_displayid_type_10(const unsigned char *x, unsigned length);
392 void cta_block(const unsigned char *x, std::vector<unsigned> &found_tags);
393 void preparse_cta_block(const unsigned char *x);
394 void parse_cta_block(const unsigned char *x);
395 void cta_resolve_svr(vec_timings_ext::iterator iter);
396 void cta_resolve_svrs();
397 void check_cta_blocks();
398 void cta_list_vics();
399 void cta_list_hdmi_vics();
400 void cta_list_rids();
401 void cta_list_rid_timings(unsigned list_rid = 0);
403 void set_displayid_native_res(unsigned w, unsigned h);
404 void parse_digital_interface(const unsigned char *x);
405 void parse_display_device(const unsigned char *x);
406 void parse_display_caps(const unsigned char *x);
407 void parse_display_xfer(const unsigned char *x);
408 void parse_di_ext_block(const unsigned char *x);
410 void check_displayid_datablock_revision(unsigned char hdr,
411 unsigned char valid_flags = 0,
412 unsigned char rev = 0);
413 void parse_displayid_product_id(const unsigned char *x);
414 std::string product_type(unsigned char x, bool heading);
415 void parse_displayid_interface_features(const unsigned char *x);
416 void parse_displayid_parameters(const unsigned char *x);
417 void parse_displayid_parameters_v2(const unsigned char *x, unsigned block_rev);
418 void parse_displayid_display_intf(const unsigned char *x);
419 void parse_displayid_color_characteristics(const unsigned char *x);
420 void parse_displayid_transfer_characteristics(const unsigned char *x);
421 void parse_displayid_stereo_display_intf(const unsigned char *x);
422 void parse_displayid_type_1_7_timing(const unsigned char *x,
423 bool type7, unsigned block_rev, bool is_cta = false);
424 void parse_displayid_type_2_timing(const unsigned char *x);
425 void parse_displayid_type_3_timing(const unsigned char *x);
426 void parse_displayid_type_4_8_timing(unsigned char type, unsigned short id, bool is_cta = false);
427 void parse_displayid_video_timing_range_limits(const unsigned char *x);
428 void parse_displayid_string(const unsigned char *x);
429 void parse_displayid_display_device(const unsigned char *x);
430 void parse_displayid_intf_power_sequencing(const unsigned char *x);
431 void parse_displayid_type_5_timing(const unsigned char *x);
432 void parse_displayid_tiled_display_topology(const unsigned char *x, bool is_v2);
433 void parse_displayid_type_6_timing(const unsigned char *x);
434 void parse_displayid_type_9_timing(const unsigned char *x);
435 void parse_displayid_dynamic_video_timings_range_limits(const unsigned char *x);
436 void parse_displayid_ContainerID(const unsigned char *x);
437 void parse_displayid_adaptive_sync(const unsigned char *x);
438 void parse_displayid_type_10_timing(const unsigned char *x, unsigned sz,
439 bool is_cta = false);
440 void preparse_displayid_block(const unsigned char *x);
441 unsigned displayid_block(const unsigned version, const unsigned char *x, unsigned length);
442 void parse_displayid_block(const unsigned char *x);
443 void parse_displayid_vesa(const unsigned char *x);
444 void parse_displayid_apple(const unsigned char *x);
445 void parse_displayid_cta_data_block(const unsigned char *x);
446 void check_displayid_blocks();
448 void parse_vtb_ext_block(const unsigned char *x);
450 void parse_string_table(const unsigned char *x);
451 void parse_ls_ext_block(const unsigned char *x);
453 void parse_block_map(const unsigned char *x);
455 void preparse_extension(const unsigned char *x);
456 void parse_extension(const unsigned char *x);
457 void print_preferred_timings();
458 void print_native_res();
459 int parse_edid();
462 static inline void add_str(std::string &s, const std::string &add)
464 if (s.empty())
465 s = add;
466 else if (!add.empty())
467 s = s + ", " + add;
470 void msg(bool is_warn, const char *fmt, ...);
472 #ifdef _WIN32
474 #define warn(fmt, ...) msg(true, fmt, __VA_ARGS__)
475 #define warn_once(fmt, ...) \
476 do { \
477 static bool shown_warn; \
478 if (!shown_warn) { \
479 shown_warn = true; \
480 msg(true, fmt, __VA_ARGS__); \
482 } while (0)
483 #define fail(fmt, ...) msg(false, fmt, __VA_ARGS__)
485 #else
487 #define warn(fmt, args...) msg(true, fmt, ##args)
488 #define warn_once(fmt, args...) \
489 do { \
490 static bool shown_warn; \
491 if (!shown_warn) { \
492 shown_warn = true; \
493 msg(true, fmt, ##args); \
495 } while (0)
496 #define fail(fmt, args...) msg(false, fmt, ##args)
498 #endif
500 void do_checksum(const char *prefix, const unsigned char *x, size_t len, unsigned unused_bytes = 0);
501 std::string utohex(unsigned char x);
502 std::string ouitohex(unsigned oui);
503 std::string containerid2s(const unsigned char *x);
504 bool memchk(const unsigned char *x, unsigned len, unsigned char v = 0);
505 void hex_block(const char *prefix, const unsigned char *x, unsigned length,
506 bool show_ascii = true, unsigned step = 16);
507 std::string block_name(unsigned char block);
508 void calc_ratio(struct timings *t);
509 const char *oui_name(unsigned oui, unsigned *ouinum = NULL);
510 unsigned gcd(unsigned a, unsigned b);
512 bool timings_close_match(const timings &t1, const timings &t2);
513 const struct timings *find_dmt_id(unsigned char dmt_id);
514 const struct timings *close_match_to_dmt(const timings &t, unsigned &dmt);
515 const struct timings *find_vic_id(unsigned char vic);
516 const struct cta_rid *find_rid(unsigned char rid);
517 const struct timings *find_hdmi_vic_id(unsigned char hdmi_vic);
518 const struct timings *cta_close_match_to_vic(const timings &t, unsigned &vic);
519 unsigned char hdmi_vic_to_vic(unsigned char hdmi_vic);
520 char *extract_string(const unsigned char *x, unsigned len);
522 #define oneoui(c,k,n) const unsigned kOUI_##k = __LINE__<<12;
523 #include "oui.h"
525 #endif