edid-decode: export speaker_map as cta_speaker_map
[edid-decode.git] / edid-decode.h
blobfc8c8fafb8d8f12b9a363d2e54a956b025cf6b43
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 // Note: for now we do not support native DisplayID data,
148 // so this is always false. But some tests are different
149 // depending on whether it is a native DisplayID structure
150 // or an extension block, so they can use this bool.
151 native_dispid = false;
152 hide_serial_numbers = false;
153 replace_unique_ids = false;
154 image_width = image_height = diagonal = 0;
155 serial_string_cnt = 0;
156 serial_strings.clear();
158 // Base block state
159 base.edid_minor = 0;
160 base.has_name_descriptor = base.has_display_range_descriptor =
161 base.supports_continuous_freq = base.supports_gtf =
162 base.supports_cvt = base.seen_non_detailed_descriptor =
163 base.has_640x480p60_est_timing = base.has_spwg =
164 base.preferred_is_also_native = false;
165 base.serial_number = 0;
166 base.week = base.year = 0;
167 base.supports_sec_gtf = false;
168 base.sec_gtf_start_freq = 0;
169 base.C = base.M = base.K = base.J = 0;
170 base.max_pos_neg_hor_freq_khz = 0;
171 base.uses_srgb = false;
172 base.detailed_block_cnt = base.dtd_cnt = 0;
174 base.min_display_hor_freq_hz = base.max_display_hor_freq_hz =
175 base.min_display_vert_freq_hz = base.max_display_vert_freq_hz =
176 base.max_display_pixclk_khz = base.max_display_width_mm =
177 base.max_display_height_mm = 0;
179 // CTA-861 block state
180 cta.has_vic_1 = cta.first_svd_might_be_preferred = cta.has_sldb =
181 cta.has_hdmi = cta.has_vcdb = cta.has_vfpdb = cta.has_cdb =
182 cta.has_nvrdb = cta.has_pidb = false;
183 cta.previous_cta_tag = 0xfff;
184 cta.have_hf_vsdb = cta.have_hf_scdb = false;
185 cta.hdmi_max_rate = 0;
186 cta.hf_eeodb_blocks = 0;
187 cta.image_width = cta.image_height = 0;
188 cta.block_number = 0;
189 cta.has_svrs = false;
190 cta.first_svd = true;
191 cta.supported_hdmi_vic_codes = cta.supported_hdmi_vic_vsb_codes = 0;
192 memset(cta.vics, 0, sizeof(cta.vics));
193 memset(cta.preparsed_has_vic, 0, sizeof(cta.preparsed_has_vic));
194 memset(&cta.preparsed_first_vfd, 0, sizeof(cta.preparsed_first_vfd));
195 cta.preparsed_phys_addr = 0xffff;
196 cta.preparsed_speaker_count = 0;
197 cta.preparsed_sld = false;
198 cta.preparsed_sld_has_coord = false;
199 cta.preparsed_total_dtds = 0;
200 cta.preparsed_total_vtdbs = 0;
201 cta.preparsed_has_t8vtdb = false;
202 cta.preparsed_t8vtdb_dmt = 0;
203 cta.preparsed_max_vic_pixclk_khz = 0;
204 cta.warn_about_hdmi_2x_dtd = false;
206 // DisplayID block state
207 dispid.version = 0;
208 dispid.native_width = dispid.native_height = 0;
209 dispid.preparsed_color_ids = dispid.preparsed_xfer_ids = 0;
210 dispid.preparsed_displayid_blocks = 0;
211 dispid.is_base_block = true;
212 dispid.is_display = dispid.has_product_identification =
213 dispid.has_display_parameters = dispid.has_type_1_7 =
214 dispid.has_display_interface_features =
215 dispid.has_tiled_display_topology = dispid.has_ycbcr_420 =
216 dispid.is_arvr = dispid.has_arvr_hdm = dispid.has_arvr_layer =
217 dispid.has_stereo = dispid.has_stereo_display_interface = false;
218 dispid.block_number = 0;
219 dispid.image_width = dispid.image_height = 0;
221 // Block Map block state
222 block_map.saw_block_1 = false;
223 block_map.saw_block_128 = false;
226 // Global state
227 unsigned edid_size;
228 unsigned num_blocks;
229 unsigned block_nr;
230 std::string block;
231 std::string data_block;
232 unsigned unused_bytes;
233 bool has_cta;
234 bool has_dispid;
235 bool native_dispid;
236 bool hide_serial_numbers;
237 bool replace_unique_ids;
238 std::vector<std::string> serial_strings;
239 unsigned serial_string_cnt;
241 unsigned min_hor_freq_hz;
242 unsigned max_hor_freq_hz;
243 double min_vert_freq_hz;
244 double max_vert_freq_hz;
245 unsigned max_pixclk_khz;
246 unsigned dtd_max_hsize_mm;
247 unsigned dtd_max_vsize_mm;
249 // in 0.1 mm units
250 unsigned image_width, image_height;
251 // in inches
252 double diagonal;
254 unsigned warnings;
255 unsigned failures;
257 // Base block state
258 struct {
259 unsigned edid_minor;
260 bool has_name_descriptor;
261 bool has_display_range_descriptor;
262 unsigned serial_number;
263 unsigned char week, year;
264 bool supports_continuous_freq;
265 bool supports_gtf;
266 bool supports_sec_gtf;
267 unsigned sec_gtf_start_freq;
268 double C, M, K, J;
269 bool supports_cvt;
270 bool has_spwg;
271 bool uses_srgb;
272 unsigned detailed_block_cnt;
273 unsigned dtd_cnt;
274 bool seen_non_detailed_descriptor;
275 bool has_640x480p60_est_timing;
276 bool preferred_is_also_native;
277 timings_ext preferred_timing;
279 unsigned min_display_hor_freq_hz;
280 unsigned max_display_hor_freq_hz;
281 unsigned min_display_vert_freq_hz;
282 unsigned max_display_vert_freq_hz;
283 unsigned max_display_pixclk_khz;
284 unsigned max_display_width_mm;
285 unsigned max_display_height_mm;
286 unsigned max_pos_neg_hor_freq_khz;
287 } base;
289 // CTA-861 block state
290 struct {
291 unsigned preparsed_total_dtds;
292 vec_timings_ext vec_dtds;
293 unsigned preparsed_total_vtdbs;
294 vec_timings_ext vec_vtdbs;
295 cta_vfd preparsed_first_vfd;
296 vec_timings_ext preferred_timings;
297 vec_timings_ext preferred_timings_vfpdb;
298 bool preparsed_has_t8vtdb;
299 unsigned preparsed_t8vtdb_dmt;
300 // Keep track of the found Tag/Extended Tag pairs.
301 // The unsigned value is equal to: (tag) | (OUI enum << 12) or (extended tag) | (tag << 8) | (OUI enum << 12)
302 std::vector<unsigned> found_tags;
303 timings_ext t8vtdb;
304 vec_timings_ext native_timings;
305 vec_timings_ext native_timing_nvrdb;
306 // in 0.1 mm units
307 unsigned image_width, image_height;
308 bool has_vic_1;
309 bool first_svd_might_be_preferred;
310 unsigned char byte3;
311 bool has_hdmi;
312 unsigned hdmi_max_rate;
313 bool has_vcdb;
314 bool has_vfpdb;
315 bool has_nvrdb;
316 bool has_cdb;
317 unsigned preparsed_speaker_count;
318 bool preparsed_sld_has_coord;
319 bool preparsed_sld;
320 bool has_sldb;
321 bool has_pidb;
322 unsigned short preparsed_phys_addr;
323 unsigned previous_cta_tag;
324 bool have_hf_vsdb, have_hf_scdb;
325 unsigned hf_eeodb_blocks;
326 unsigned block_number;
327 bool has_svrs;
328 bool first_svd;
329 unsigned supported_hdmi_vic_codes;
330 unsigned supported_hdmi_vic_vsb_codes;
331 unsigned short vics[256][2];
332 bool preparsed_has_vic[2][256];
333 std::vector<unsigned char> preparsed_svds[2];
334 unsigned preparsed_max_vic_pixclk_khz;
335 bool warn_about_hdmi_2x_dtd;
336 } cta;
338 // DisplayID block state
339 struct {
340 unsigned char version;
341 unsigned short preparsed_color_ids;
342 unsigned short preparsed_xfer_ids;
343 unsigned preparsed_displayid_blocks;
344 bool is_base_block;
345 bool is_display;
346 bool is_arvr;
347 bool has_product_identification;
348 bool has_display_parameters;
349 bool has_type_1_7;
350 bool has_display_interface_features;
351 bool has_tiled_display_topology;
352 bool has_stereo_display_interface;
353 bool has_arvr_hdm;
354 bool has_arvr_layer;
355 bool has_ycbcr_420;
356 bool has_stereo;
357 vec_timings_ext preferred_timings;
358 unsigned native_width, native_height;
359 // in 0.1 mm units
360 unsigned image_width, image_height;
361 unsigned block_number;
362 // Keep track of the found CTA-861 Tag/Extended Tag pairs.
363 // The unsigned value is equal to: (tag) | (OUI enum << 12) or (extended tag) | (tag << 8) | (OUI enum << 12)
364 std::vector<unsigned> found_tags;
365 } dispid;
367 // Block Map block state
368 struct {
369 bool saw_block_1;
370 bool saw_block_128;
371 } block_map;
373 std::string dtd_type(unsigned dtd);
374 std::string dtd_type() { return dtd_type(base.dtd_cnt); }
375 bool print_timings(const char *prefix, const struct timings *t,
376 const char *type, const char *flags = "",
377 bool detailed = false, bool do_checks = true,
378 unsigned ntsc = 2);
379 bool print_timings(const char *prefix, const struct timings_ext &t,
380 bool detailed = false, bool do_checks = true,
381 unsigned ntsc = 2)
383 return print_timings(prefix, &t.t, t.type.c_str(), t.flags.c_str(),
384 detailed, do_checks, ntsc);
386 timings calc_gtf_mode(unsigned h_pixels, unsigned v_lines,
387 double ip_freq_rqd, bool int_rqd = false,
388 enum gtf_ip_parm ip_parm = gtf_ip_vert_freq,
389 bool margins_rqd = false, bool secondary = false,
390 double C = 40, double M = 600, double K = 128, double J = 20);
391 void edid_gtf_mode(unsigned refresh, struct timings &t);
392 timings calc_cvt_mode(unsigned h_pixels, unsigned v_lines,
393 double ip_freq_rqd, unsigned rb, bool int_rqd = false,
394 bool margins_rqd = false, bool alt = false,
395 unsigned rb_h_blank = 0, unsigned rb_v_blank = 460,
396 bool early_vsync_rqd = false);
397 void edid_cvt_mode(unsigned refresh, struct timings &t, unsigned rb_h_blank = 0,
398 unsigned rb_v_blank = 460, bool early_vsync_rqd = false);
399 void detailed_cvt_descriptor(const char *prefix, const unsigned char *x, bool first);
400 timings calc_ovt_mode(unsigned hact, unsigned vact,
401 unsigned hratio, unsigned vratio,
402 unsigned frame_rate);
403 void print_standard_timing(const char *prefix, unsigned char b1, unsigned char b2,
404 bool gtf_only = false, bool show_both = false);
405 void detailed_display_range_limits(const unsigned char *x);
406 void detailed_epi(const unsigned char *x);
407 void detailed_timings(const char *prefix, const unsigned char *x,
408 bool base_or_cta = true);
409 bool preparse_detailed_block(unsigned char *x);
410 void preparse_base_block(unsigned char *x);
411 void detailed_block(const unsigned char *x);
412 void parse_base_block(const unsigned char *x);
413 void check_base_block(const unsigned char *x);
414 void list_dmts();
415 void list_established_timings();
417 void data_block_oui(std::string block_name, const unsigned char *x, unsigned length, unsigned *ouinum,
418 bool ignorezeros = false, bool do_ascii = false, bool big_endian = false);
420 void print_vic_index(const char *prefix, unsigned idx, const char *suffix, bool ycbcr420 = false);
421 void hdmi_latency(unsigned char vid_lat, unsigned char aud_lat, bool is_ilaced);
422 void cta_vcdb(const unsigned char *x, unsigned length);
423 void cta_svd(const unsigned char *x, unsigned n, bool for_ycbcr420);
424 void cta_vfdb(const unsigned char *x, unsigned n);
425 void cta_y420cmdb(const unsigned char *x, unsigned length);
426 void cta_print_svr(unsigned char svr, vec_timings_ext &vec_tim);
427 void cta_vfpdb(const unsigned char *x, unsigned length);
428 void cta_nvrdb(const unsigned char *x, unsigned length);
429 cta_vfd cta_parse_vfd(const unsigned char *x, unsigned lvfd);
430 void cta_rcdb(const unsigned char *x, unsigned length);
431 void cta_sldb(const unsigned char *x, unsigned length);
432 void cta_pidb(const unsigned char *x, unsigned length);
433 void cta_preparse_sldb(const unsigned char *x, unsigned length);
434 void cta_colorimetry_block(const unsigned char *x, unsigned length);
435 void cta_hdmi_block(const unsigned char *x, unsigned length);
436 void cta_hf_scdb(const unsigned char *x, unsigned length);
437 void cta_displayid_type_7(const unsigned char *x, unsigned length);
438 void cta_displayid_type_8(const unsigned char *x, unsigned length);
439 void cta_displayid_type_10(const unsigned char *x, unsigned length);
440 void cta_block(const unsigned char *x, std::vector<unsigned> &found_tags);
441 void preparse_cta_block(unsigned char *x);
442 void parse_cta_block(const unsigned char *x);
443 void cta_resolve_svr(timings_ext &t_ext);
444 void cta_resolve_svrs();
445 void check_cta_blocks();
446 void cta_list_vics();
447 void cta_list_hdmi_vics();
448 void cta_list_rids();
449 void cta_list_rid_timings(unsigned list_rid = 0);
451 void set_displayid_native_res(unsigned w, unsigned h);
452 void parse_digital_interface(const unsigned char *x);
453 void parse_display_device(const unsigned char *x);
454 void parse_display_caps(const unsigned char *x);
455 void parse_display_xfer(const unsigned char *x);
456 void parse_di_ext_block(const unsigned char *x);
458 void check_displayid_datablock_revision(unsigned char hdr,
459 unsigned char valid_flags = 0,
460 unsigned char rev = 0);
461 void parse_displayid_product_id(const unsigned char *x);
462 std::string product_type(unsigned char x, bool heading);
463 void parse_displayid_interface_features(const unsigned char *x);
464 void parse_displayid_parameters(const unsigned char *x);
465 void parse_displayid_parameters_v2(const unsigned char *x, unsigned block_rev);
466 void parse_displayid_display_intf(const unsigned char *x);
467 void parse_displayid_color_characteristics(const unsigned char *x);
468 void parse_displayid_transfer_characteristics(const unsigned char *x);
469 void parse_displayid_stereo_display_intf(const unsigned char *x);
470 void parse_displayid_type_1_7_timing(const unsigned char *x,
471 bool type7, unsigned block_rev, bool is_cta = false);
472 void parse_displayid_type_2_timing(const unsigned char *x);
473 void parse_displayid_type_3_timing(const unsigned char *x);
474 void parse_displayid_type_4_8_timing(unsigned char type, unsigned short id, bool is_cta = false);
475 void parse_displayid_video_timing_range_limits(const unsigned char *x);
476 void parse_displayid_string(const unsigned char *x);
477 void parse_displayid_display_device(const unsigned char *x);
478 void parse_displayid_intf_power_sequencing(const unsigned char *x);
479 void parse_displayid_type_5_timing(const unsigned char *x);
480 void parse_displayid_tiled_display_topology(const unsigned char *x, bool is_v2);
481 void parse_displayid_type_6_timing(const unsigned char *x);
482 void parse_displayid_type_9_timing(const unsigned char *x);
483 void parse_displayid_dynamic_video_timings_range_limits(const unsigned char *x);
484 void parse_displayid_ContainerID(const unsigned char *x);
485 void parse_displayid_adaptive_sync(const unsigned char *x);
486 void parse_displayid_arvr_hmd(const unsigned char *x);
487 void parse_displayid_arvr_layer(const unsigned char *x);
488 void parse_displayid_brightness_lum_range(const unsigned char *x);
489 void parse_displayid_type_10_timing(const unsigned char *x, unsigned sz,
490 bool is_cta = false);
491 void preparse_displayid_block(unsigned char *x);
492 unsigned displayid_block(const unsigned version, const unsigned char *x, unsigned length);
493 void parse_displayid_block(const unsigned char *x);
494 void parse_displayid_vesa(const unsigned char *x);
495 void parse_displayid_apple(const unsigned char *x);
496 void parse_displayid_cta_data_block(const unsigned char *x);
497 void check_displayid_blocks();
499 void parse_vtb_ext_block(const unsigned char *x);
501 void parse_string_table(const unsigned char *x);
502 void preparse_ls_ext_block(unsigned char *x);
503 void parse_ls_ext_block(const unsigned char *x);
505 void parse_block_map(const unsigned char *x);
507 void preparse_extension(unsigned char *x);
508 void parse_extension(const unsigned char *x);
509 void print_preferred_timings();
510 void print_native_res();
511 int parse_edid();
514 static inline void add_str(std::string &s, const std::string &add)
516 if (s.empty())
517 s = add;
518 else if (!add.empty())
519 s = s + ", " + add;
522 void msg(bool is_warn, const char *fmt, ...);
524 #ifdef _WIN32
526 #define warn(fmt, ...) msg(true, fmt, __VA_ARGS__)
527 #define warn_once(fmt, ...) \
528 do { \
529 static bool shown_warn; \
530 if (!shown_warn) { \
531 shown_warn = true; \
532 msg(true, fmt, __VA_ARGS__); \
534 } while (0)
535 #define fail(fmt, ...) msg(false, fmt, __VA_ARGS__)
537 #else
539 #define warn(fmt, args...) msg(true, fmt, ##args)
540 #define warn_once(fmt, args...) \
541 do { \
542 static bool shown_warn; \
543 if (!shown_warn) { \
544 shown_warn = true; \
545 msg(true, fmt, ##args); \
547 } while (0)
548 #define fail(fmt, args...) msg(false, fmt, ##args)
550 #endif
552 // NULL terminated array
553 extern const char *cta_speaker_map[];
555 std::string utohex(unsigned char x);
556 std::string ouitohex(unsigned oui);
557 std::string containerid2s(const unsigned char *x);
558 bool memchk(const unsigned char *x, unsigned len, unsigned char v = 0);
559 void hex_block(const char *prefix, const unsigned char *x, unsigned length,
560 bool show_ascii = true, unsigned step = 16);
561 std::string block_name(unsigned char block);
562 void do_checksum(const char *prefix, const unsigned char *x, size_t len, size_t checksum_pos,
563 unsigned unused_bytes = 0);
564 void replace_checksum(unsigned char *x, size_t len);
565 void calc_ratio(struct timings *t);
566 const char *oui_name(unsigned oui, unsigned *ouinum = NULL);
567 unsigned gcd(unsigned a, unsigned b);
569 bool match_timings(const timings &t1, const timings &t2);
570 bool timings_close_match(const timings &t1, const timings &t2);
571 const struct timings *find_dmt_id(unsigned char dmt_id);
572 const struct timings *close_match_to_dmt(const timings &t, unsigned &dmt);
573 const struct timings *find_vic_id(unsigned char vic);
574 const struct cta_rid *find_rid(unsigned char rid);
575 const struct timings *find_hdmi_vic_id(unsigned char hdmi_vic);
576 const struct timings *cta_close_match_to_vic(const timings &t, unsigned &vic);
577 bool cta_matches_vic(const timings &t, unsigned &vic);
578 unsigned char hdmi_vic_to_vic(unsigned char hdmi_vic);
579 char *extract_string(const unsigned char *x, unsigned len);
581 #define oneoui(c,k,n) const unsigned kOUI_##k = __LINE__<<12;
582 #include "oui.h"
584 #endif