edid-decode: the wrong S_IT bits were read for VCDB
[edid-decode.git] / edid-decode.h
blob9c2044d89cff8d299050fc9252b11003711e5809
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 <string.h>
17 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
18 #define min(a, b) ((a) < (b) ? (a) : (b))
19 #define max(a, b) ((a) > (b) ? (a) : (b))
21 #define EDID_PAGE_SIZE 128U
22 #define EDID_MAX_BLOCKS 256U
24 // Video Timings
25 // If interlaced is true, then the vertical blanking
26 // for each field is (vfp + vsync + vbp + 0.5), except for
27 // the VIC 39 timings that doesn't have the 0.5 constant.
28 struct timings {
29 // Active horizontal and vertical frame height, including any
30 // borders, if present.
31 // Note: for interlaced formats the active field height is vact / 2
32 unsigned hact, vact;
33 unsigned hratio, vratio;
34 unsigned pixclk_khz;
35 unsigned rb; // 1 if CVT with reduced blanking, 2 if CVT with reduced blanking v2
36 bool interlaced;
37 // The horizontal frontporch may be negative in GTF calculations,
38 // so use int instead of unsigned for hfp. Example: 292x176@76.
39 int hfp;
40 unsigned hsync;
41 // The backporch may be negative in buggy detailed timings.
42 // So use int instead of unsigned for hbp and vbp.
43 int hbp;
44 bool pos_pol_hsync;
45 // For interlaced formats the vertical front porch of the Even Field
46 // is actually a half-line longer.
47 unsigned vfp, vsync;
48 // For interlaced formats the vertical back porch of the Odd Field
49 // is actually a half-line longer.
50 int vbp;
51 bool pos_pol_vsync;
52 unsigned hborder, vborder;
53 bool even_vtotal; // special for VIC 39
54 bool no_pol_vsync; // digital composite signals have no vsync polarity
55 unsigned hsize_mm, vsize_mm;
56 bool ycbcr420; // YCbCr 4:2:0 encoding
59 struct timings_ext {
60 timings_ext()
62 memset(&t, 0, sizeof(t));
64 timings_ext(unsigned svr, const std::string &_type)
66 memset(&t, 0, sizeof(t));
67 t.hact = svr;
68 type = _type;
70 timings_ext(const timings &_t, const std::string &_type, const std::string &_flags)
72 t = _t;
73 type = _type;
74 flags = _flags;
77 bool is_valid() const { return t.hact; }
78 bool has_svr() const { return t.hact && !t.vact; }
79 unsigned svr() const { return t.hact; }
80 timings t;
81 std::string type;
82 std::string flags;
85 typedef std::vector<timings_ext> vec_timings_ext;
87 struct edid_state {
88 edid_state()
90 // Global state
91 edid_size = num_blocks = block_nr = 0;
92 max_hor_freq_hz = max_vert_freq_hz = max_pixclk_khz = 0;
93 min_hor_freq_hz = 0xffffff;
94 min_vert_freq_hz = 0xffffffff;
95 warnings = failures = 0;
96 has_cta = has_dispid = false;
98 // Base block state
99 base.edid_minor = 0;
100 base.has_name_descriptor = base.has_display_range_descriptor =
101 base.has_serial_number = base.has_serial_string =
102 base.supports_continuous_freq = base.supports_gtf =
103 base.supports_cvt = base.uses_gtf = base.uses_cvt =
104 base.has_640x480p60_est_timing = base.has_spwg =
105 base.seen_non_detailed_descriptor =
106 base.preferred_is_also_native = false;
107 base.detailed_block_cnt = base.dtd_cnt = 0;
109 base.min_display_hor_freq_hz = base.max_display_hor_freq_hz =
110 base.min_display_vert_freq_hz = base.max_display_vert_freq_hz =
111 base.max_display_pixclk_khz = base.max_display_width_mm =
112 base.max_display_height_mm = 0;
114 // CTA-861 block state
115 cta.has_vic_1 = cta.first_svd_might_be_preferred =
116 cta.has_hdmi = cta.has_vcdb = cta.has_vfpdb = false;
117 cta.last_block_was_hdmi_vsdb = cta.have_hf_vsdb = cta.have_hf_scdb = false;
118 cta.first_block = cta.first_svd = true;
119 cta.supported_hdmi_vic_codes = cta.supported_hdmi_vic_vsb_codes = 0;
120 memset(cta.vics, 0, sizeof(cta.vics));
121 memset(cta.preparsed_has_vic, 0, sizeof(cta.preparsed_has_vic));
122 cta.preparsed_phys_addr = 0xffff;
123 cta.preparse_total_dtds = 0;
125 // DisplayID block state
126 dispid.version = 0;
127 dispid.preparse_color_ids = dispid.preparse_xfer_ids = 0;
128 dispid.preparse_displayid_blocks = 0;
129 dispid.is_base_block = true;
130 dispid.is_display = dispid.has_product_identification =
131 dispid.has_display_parameters = dispid.has_type_1_7 =
132 dispid.has_display_interface_features = false;
134 // Block Map block state
135 block_map.saw_block_1 = false;
138 // Global state
139 unsigned edid_size;
140 unsigned num_blocks;
141 unsigned block_nr;
142 std::string block;
143 std::string data_block;
144 bool has_cta;
145 bool has_dispid;
147 unsigned min_hor_freq_hz;
148 unsigned max_hor_freq_hz;
149 double min_vert_freq_hz;
150 double max_vert_freq_hz;
151 unsigned max_pixclk_khz;
153 unsigned warnings;
154 unsigned failures;
156 // Base block state
157 struct {
158 unsigned edid_minor;
159 bool has_name_descriptor;
160 bool has_display_range_descriptor;
161 bool has_serial_number;
162 bool has_serial_string;
163 bool supports_continuous_freq;
164 bool supports_gtf;
165 bool supports_cvt;
166 bool uses_gtf;
167 bool uses_cvt;
168 bool has_spwg;
169 unsigned detailed_block_cnt;
170 unsigned dtd_cnt;
171 bool seen_non_detailed_descriptor;
172 bool has_640x480p60_est_timing;
173 bool preferred_is_also_native;
174 timings_ext preferred_timing;
176 unsigned min_display_hor_freq_hz;
177 unsigned max_display_hor_freq_hz;
178 unsigned min_display_vert_freq_hz;
179 unsigned max_display_vert_freq_hz;
180 unsigned max_display_pixclk_khz;
181 unsigned max_display_width_mm;
182 unsigned max_display_height_mm;
183 } base;
185 // CTA-861 block state
186 struct {
187 unsigned preparse_total_dtds;
188 vec_timings_ext vec_dtds;
189 vec_timings_ext preferred_timings;
190 vec_timings_ext native_timings;
191 bool has_vic_1;
192 bool first_svd_might_be_preferred;
193 unsigned char byte3;
194 bool has_hdmi;
195 bool has_vcdb;
196 bool has_vfpdb;
197 unsigned short preparsed_phys_addr;
198 bool last_block_was_hdmi_vsdb;
199 bool have_hf_vsdb, have_hf_scdb;
200 bool first_block;
201 bool first_svd;
202 unsigned supported_hdmi_vic_codes;
203 unsigned supported_hdmi_vic_vsb_codes;
204 unsigned short vics[256][2];
205 bool preparsed_has_vic[2][256];
206 std::vector<unsigned char> preparsed_svds[2];
207 } cta;
209 // DisplayID block state
210 struct {
211 unsigned char version;
212 unsigned short preparse_color_ids;
213 unsigned short preparse_xfer_ids;
214 unsigned preparse_displayid_blocks;
215 bool is_base_block;
216 bool is_display;
217 bool has_product_identification;
218 bool has_display_parameters;
219 bool has_type_1_7;
220 bool has_display_interface_features;
221 vec_timings_ext preferred_timings;
222 } dispid;
224 // Block Map block state
225 struct {
226 bool saw_block_1;
227 } block_map;
229 std::string dtd_type(unsigned dtd);
230 std::string dtd_type() { return dtd_type(base.dtd_cnt); }
231 bool print_timings(const char *prefix, const struct timings *t,
232 const char *type, const char *flags = "",
233 bool detailed = false);
234 bool print_timings(const char *prefix, const struct timings_ext &t,
235 bool detailed = false)
237 return print_timings(prefix, &t.t, t.type.c_str(), t.flags.c_str(), detailed);
239 void edid_gtf_mode(unsigned refresh, struct timings &t);
240 void edid_cvt_mode(unsigned refresh, struct timings &t);
241 void detailed_cvt_descriptor(const char *prefix, const unsigned char *x, bool first);
242 void print_standard_timing(const char *prefix, unsigned char b1, unsigned char b2,
243 bool gtf_only = false, unsigned vrefresh_offset = 60);
244 void detailed_display_range_limits(const unsigned char *x);
245 void detailed_epi(const unsigned char *x);
246 void detailed_timings(const char *prefix, const unsigned char *x,
247 bool base_or_cta = true);
248 void detailed_block(const unsigned char *x);
249 void parse_base_block(const unsigned char *x);
250 void check_base_block();
252 void print_vic_index(const char *prefix, unsigned idx, const char *suffix, bool ycbcr420 = false);
253 void cta_vcdb(const unsigned char *x, unsigned length);
254 void cta_svd(const unsigned char *x, unsigned n, bool for_ycbcr420);
255 void cta_y420cmdb(const unsigned char *x, unsigned length);
256 void cta_vfpdb(const unsigned char *x, unsigned length);
257 void cta_hdmi_block(const unsigned char *x, unsigned length);
258 void cta_ext_block(const unsigned char *x, unsigned length);
259 void cta_block(const unsigned char *x);
260 void preparse_cta_block(const unsigned char *x);
261 void parse_cta_block(const unsigned char *x);
262 void cta_resolve_svr(vec_timings_ext::iterator iter);
263 void cta_resolve_svrs();
264 void check_cta_blocks();
266 void parse_digital_interface(const unsigned char *x);
267 void parse_display_device(const unsigned char *x);
268 void parse_display_caps(const unsigned char *x);
269 void parse_display_xfer(const unsigned char *x);
270 void parse_di_ext_block(const unsigned char *x);
272 void parse_displayid_product_id(const unsigned char *x);
273 std::string product_type(unsigned char x, bool heading);
274 void parse_displayid_interface_features(const unsigned char *x);
275 void parse_displayid_parameters(const unsigned char *x);
276 void parse_displayid_parameters_v2(const unsigned char *x);
277 void parse_displayid_display_intf(const unsigned char *x);
278 void parse_displayid_color_characteristics(const unsigned char *x);
279 void parse_displayid_transfer_characteristics(const unsigned char *x);
280 void parse_displayid_stereo_display_intf(const unsigned char *x);
281 void parse_displayid_type_1_7_timing(const unsigned char *x, bool type7);
282 void parse_displayid_type_2_timing(const unsigned char *x);
283 void parse_displayid_type_3_timing(const unsigned char *x);
284 void parse_displayid_type_4_8_timing(unsigned char type, unsigned short id);
285 void parse_displayid_type_5_timing(const unsigned char *x);
286 void parse_displayid_type_6_timing(const unsigned char *x);
287 void parse_displayid_type_9_timing(const unsigned char *x);
288 void preparse_displayid_block(const unsigned char *x);
289 void parse_displayid_block(const unsigned char *x);
290 void parse_displayid_cta_data_block(const unsigned char *x);
291 void check_displayid_blocks();
293 void parse_vtb_ext_block(const unsigned char *x);
295 void parse_ls_ext_block(const unsigned char *x);
297 void parse_block_map(const unsigned char *x);
299 void preparse_extension(const unsigned char *x);
300 void parse_extension(const unsigned char *x);
301 int parse_edid();
304 static inline void add_str(std::string &s, const std::string &add)
306 if (s.empty())
307 s = add;
308 else if (!add.empty())
309 s = s + ", " + add;
312 void msg(bool is_warn, const char *fmt, ...);
314 #ifdef _WIN32
316 #define warn(fmt, ...) msg(true, fmt, __VA_ARGS__)
317 #define warn_once(fmt, ...) \
318 do { \
319 static bool shown_warn; \
320 if (!shown_warn) { \
321 shown_warn = true; \
322 msg(true, fmt, __VA_ARGS__); \
324 } while (0)
325 #define fail(fmt, ...) msg(false, fmt, __VA_ARGS__)
327 #else
329 #define warn(fmt, args...) msg(true, fmt, ##args)
330 #define warn_once(fmt, args...) \
331 do { \
332 static bool shown_warn; \
333 if (!shown_warn) { \
334 shown_warn = true; \
335 msg(true, fmt, ##args); \
337 } while (0)
338 #define fail(fmt, args...) msg(false, fmt, ##args)
340 #endif
342 void do_checksum(const char *prefix, const unsigned char *x, size_t len);
343 std::string utohex(unsigned char x);
344 std::string ouitohex(unsigned oui);
345 bool memchk(const unsigned char *x, unsigned len, unsigned char v = 0);
346 void hex_block(const char *prefix, const unsigned char *x, unsigned length,
347 bool show_ascii = true, unsigned step = 16);
348 std::string block_name(unsigned char block);
349 void calc_ratio(struct timings *t);
350 const char *oui_name(unsigned oui, bool reverse = false);
352 const struct timings *find_dmt_id(unsigned char dmt_id);
353 const struct timings *find_vic_id(unsigned char vic);
354 const struct timings *find_hdmi_vic_id(unsigned char hdmi_vic);
355 unsigned char hdmi_vic_to_vic(unsigned char hdmi_vic);
356 char *extract_string(const unsigned char *x, unsigned len);
358 #endif