1 // SPDX-License-Identifier: MIT
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>
10 #ifndef __EDID_DECODE_H_
11 #define __EDID_DECODE_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)
28 #define RB_CVT_V1 (1U)
29 #define RB_CVT_V2 (2U)
30 #define RB_CVT_V3 (3U)
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
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
54 unsigned hratio
, vratio
;
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
65 // The horizontal frontporch may be negative in GTF calculations,
66 // so use int instead of unsigned for hfp. Example: 292x176@76.
69 // The backporch may be negative in buggy detailed timings.
70 // So use int instead of unsigned for hbp and vbp.
73 // For interlaced formats the vertical front porch of the Even Field
74 // is actually a half-line longer.
76 // For interlaced formats the vertical back porch of the Odd Field
77 // is actually a half-line longer.
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
90 memset(&t
, 0, sizeof(t
));
92 timings_ext(unsigned svr
, const std::string
&_type
)
94 memset(&t
, 0, sizeof(t
));
98 timings_ext(const timings
&_t
, const std::string
&_type
, const std::string
&_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
; }
114 gtf_ip_vert_freq
= 1,
119 typedef std::vector
<timings_ext
> vec_timings_ext
;
123 unsigned hratio
, vratio
;
128 unsigned char fr_factor
;
129 unsigned int bfr50
:1;
131 unsigned int bfr60
:1;
132 unsigned int fr144
:1;
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;
153 base
.has_name_descriptor
= base
.has_display_range_descriptor
=
154 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
.serial_number
= 0;
160 base
.serial_string
[0] = 0;
161 base
.supports_sec_gtf
= false;
162 base
.sec_gtf_start_freq
= 0;
163 base
.C
= base
.M
= base
.K
= base
.J
= 0;
164 base
.max_pos_neg_hor_freq_khz
= 0;
165 base
.uses_srgb
= false;
166 base
.detailed_block_cnt
= base
.dtd_cnt
= 0;
168 base
.min_display_hor_freq_hz
= base
.max_display_hor_freq_hz
=
169 base
.min_display_vert_freq_hz
= base
.max_display_vert_freq_hz
=
170 base
.max_display_pixclk_khz
= base
.max_display_width_mm
=
171 base
.max_display_height_mm
= 0;
173 // CTA-861 block state
174 cta
.has_vic_1
= cta
.first_svd_might_be_preferred
= cta
.has_sldb
=
175 cta
.has_hdmi
= cta
.has_vcdb
= cta
.has_vfpdb
= cta
.has_cdb
=
176 cta
.has_nvrdb
= false;
177 cta
.previous_cta_tag
= 0xfff;
178 cta
.have_hf_vsdb
= cta
.have_hf_scdb
= false;
179 cta
.hf_eeodb_blocks
= 0;
180 cta
.image_width
= cta
.image_height
= 0;
181 cta
.block_number
= 0;
182 cta
.has_svrs
= false;
183 cta
.first_svd
= true;
184 cta
.supported_hdmi_vic_codes
= cta
.supported_hdmi_vic_vsb_codes
= 0;
185 memset(cta
.vics
, 0, sizeof(cta
.vics
));
186 memset(cta
.preparsed_has_vic
, 0, sizeof(cta
.preparsed_has_vic
));
187 memset(&cta
.preparsed_first_vfd
, 0, sizeof(cta
.preparsed_first_vfd
));
188 cta
.preparsed_phys_addr
= 0xffff;
189 cta
.preparsed_speaker_count
= 0;
190 cta
.preparsed_sld
= false;
191 cta
.preparsed_sld_has_coord
= false;
192 cta
.preparsed_total_dtds
= 0;
193 cta
.preparsed_total_vtdbs
= 0;
194 cta
.preparsed_has_t8vtdb
= false;
195 cta
.preparsed_t8vtdb_dmt
= 0;
196 cta
.preparsed_max_vic_pixclk_khz
= 0;
197 cta
.warn_about_hdmi_2x_dtd
= false;
199 // DisplayID block state
201 dispid
.native_width
= dispid
.native_height
= 0;
202 dispid
.preparsed_color_ids
= dispid
.preparsed_xfer_ids
= 0;
203 dispid
.preparsed_displayid_blocks
= 0;
204 dispid
.is_base_block
= true;
205 dispid
.is_display
= dispid
.has_product_identification
=
206 dispid
.has_display_parameters
= dispid
.has_type_1_7
=
207 dispid
.has_display_interface_features
= false;
208 dispid
.block_number
= 0;
209 dispid
.image_width
= dispid
.image_height
= 0;
211 // Block Map block state
212 block_map
.saw_block_1
= false;
213 block_map
.saw_block_128
= false;
221 std::string data_block
;
222 unsigned unused_bytes
;
225 bool hide_serial_numbers
;
226 bool replace_unique_ids
;
228 unsigned min_hor_freq_hz
;
229 unsigned max_hor_freq_hz
;
230 double min_vert_freq_hz
;
231 double max_vert_freq_hz
;
232 unsigned max_pixclk_khz
;
233 unsigned dtd_max_hsize_mm
;
234 unsigned dtd_max_vsize_mm
;
237 unsigned image_width
, image_height
;
247 bool has_name_descriptor
;
248 bool has_display_range_descriptor
;
249 unsigned serial_number
;
250 char serial_string
[14];
251 bool has_serial_string
;
252 bool supports_continuous_freq
;
254 bool supports_sec_gtf
;
255 unsigned sec_gtf_start_freq
;
260 unsigned detailed_block_cnt
;
262 bool seen_non_detailed_descriptor
;
263 bool has_640x480p60_est_timing
;
264 bool preferred_is_also_native
;
265 timings_ext preferred_timing
;
267 unsigned min_display_hor_freq_hz
;
268 unsigned max_display_hor_freq_hz
;
269 unsigned min_display_vert_freq_hz
;
270 unsigned max_display_vert_freq_hz
;
271 unsigned max_display_pixclk_khz
;
272 unsigned max_display_width_mm
;
273 unsigned max_display_height_mm
;
274 unsigned max_pos_neg_hor_freq_khz
;
277 // CTA-861 block state
279 unsigned preparsed_total_dtds
;
280 vec_timings_ext vec_dtds
;
281 unsigned preparsed_total_vtdbs
;
282 vec_timings_ext vec_vtdbs
;
283 cta_vfd preparsed_first_vfd
;
284 vec_timings_ext preferred_timings
;
285 vec_timings_ext preferred_timings_vfpdb
;
286 bool preparsed_has_t8vtdb
;
287 unsigned preparsed_t8vtdb_dmt
;
288 // Keep track of the found Tag/Extended Tag pairs.
289 // The unsigned value is equal to: (tag) | (OUI enum << 12) or (extended tag) | (tag << 8) | (OUI enum << 12)
290 std::vector
<unsigned> found_tags
;
292 vec_timings_ext native_timings
;
293 vec_timings_ext native_timing_nvrdb
;
295 unsigned image_width
, image_height
;
297 bool first_svd_might_be_preferred
;
304 unsigned preparsed_speaker_count
;
305 bool preparsed_sld_has_coord
;
308 unsigned short preparsed_phys_addr
;
309 unsigned previous_cta_tag
;
310 bool have_hf_vsdb
, have_hf_scdb
;
311 unsigned hf_eeodb_blocks
;
312 unsigned block_number
;
315 unsigned supported_hdmi_vic_codes
;
316 unsigned supported_hdmi_vic_vsb_codes
;
317 unsigned short vics
[256][2];
318 bool preparsed_has_vic
[2][256];
319 std::vector
<unsigned char> preparsed_svds
[2];
320 unsigned preparsed_max_vic_pixclk_khz
;
321 bool warn_about_hdmi_2x_dtd
;
324 // DisplayID block state
326 unsigned char version
;
327 unsigned short preparsed_color_ids
;
328 unsigned short preparsed_xfer_ids
;
329 unsigned preparsed_displayid_blocks
;
332 bool has_product_identification
;
333 bool has_display_parameters
;
335 bool has_display_interface_features
;
336 vec_timings_ext preferred_timings
;
337 unsigned native_width
, native_height
;
339 unsigned image_width
, image_height
;
340 unsigned block_number
;
341 // Keep track of the found CTA-861 Tag/Extended Tag pairs.
342 // The unsigned value is equal to: (tag) | (OUI enum << 12) or (extended tag) | (tag << 8) | (OUI enum << 12)
343 std::vector
<unsigned> found_tags
;
346 // Block Map block state
352 std::string
dtd_type(unsigned dtd
);
353 std::string
dtd_type() { return dtd_type(base
.dtd_cnt
); }
354 bool print_timings(const char *prefix
, const struct timings
*t
,
355 const char *type
, const char *flags
= "",
356 bool detailed
= false, bool do_checks
= true);
357 bool print_timings(const char *prefix
, const struct timings_ext
&t
,
358 bool detailed
= false, bool do_checks
= true)
360 return print_timings(prefix
, &t
.t
, t
.type
.c_str(), t
.flags
.c_str(),
361 detailed
, do_checks
);
363 timings
calc_gtf_mode(unsigned h_pixels
, unsigned v_lines
,
364 double ip_freq_rqd
, bool int_rqd
= false,
365 enum gtf_ip_parm ip_parm
= gtf_ip_vert_freq
,
366 bool margins_rqd
= false, bool secondary
= false,
367 double C
= 40, double M
= 600, double K
= 128, double J
= 20);
368 void edid_gtf_mode(unsigned refresh
, struct timings
&t
);
369 timings
calc_cvt_mode(unsigned h_pixels
, unsigned v_lines
,
370 double ip_freq_rqd
, unsigned rb
, bool int_rqd
= false,
371 bool margins_rqd
= false, bool alt
= false,
372 unsigned rb_h_blank
= 0, unsigned rb_v_blank
= 460,
373 bool early_vsync_rqd
= false);
374 void edid_cvt_mode(unsigned refresh
, struct timings
&t
, unsigned rb_h_blank
= 0,
375 unsigned rb_v_blank
= 460, bool early_vsync_rqd
= false);
376 void detailed_cvt_descriptor(const char *prefix
, const unsigned char *x
, bool first
);
377 timings
calc_ovt_mode(unsigned hact
, unsigned vact
,
378 unsigned hratio
, unsigned vratio
,
379 unsigned frame_rate
);
380 void print_standard_timing(const char *prefix
, unsigned char b1
, unsigned char b2
,
381 bool gtf_only
= false, bool show_both
= false);
382 void detailed_display_range_limits(const unsigned char *x
);
383 void detailed_epi(const unsigned char *x
);
384 void detailed_timings(const char *prefix
, const unsigned char *x
,
385 bool base_or_cta
= true);
386 bool preparse_detailed_block(unsigned char *x
);
387 void preparse_base_block(unsigned char *x
);
388 void detailed_block(const unsigned char *x
);
389 void parse_base_block(const unsigned char *x
);
390 void check_base_block(const unsigned char *x
);
392 void list_established_timings();
394 void data_block_oui(std::string block_name
, const unsigned char *x
, unsigned length
, unsigned *ouinum
,
395 bool ignorezeros
= false, bool do_ascii
= false, bool big_endian
= false);
397 void print_vic_index(const char *prefix
, unsigned idx
, const char *suffix
, bool ycbcr420
= false);
398 void hdmi_latency(unsigned char vid_lat
, unsigned char aud_lat
, bool is_ilaced
);
399 void cta_vcdb(const unsigned char *x
, unsigned length
);
400 void cta_svd(const unsigned char *x
, unsigned n
, bool for_ycbcr420
);
401 void cta_vfdb(const unsigned char *x
, unsigned n
);
402 void cta_y420cmdb(const unsigned char *x
, unsigned length
);
403 void cta_print_svr(unsigned char svr
, vec_timings_ext
&vec_tim
);
404 void cta_vfpdb(const unsigned char *x
, unsigned length
);
405 void cta_nvrdb(const unsigned char *x
, unsigned length
);
406 cta_vfd
cta_parse_vfd(const unsigned char *x
, unsigned lvfd
);
407 void cta_rcdb(const unsigned char *x
, unsigned length
);
408 void cta_sldb(const unsigned char *x
, unsigned length
);
409 void cta_preparse_sldb(const unsigned char *x
, unsigned length
);
410 void cta_colorimetry_block(const unsigned char *x
, unsigned length
);
411 void cta_hdmi_block(const unsigned char *x
, unsigned length
);
412 void cta_displayid_type_7(const unsigned char *x
, unsigned length
);
413 void cta_displayid_type_8(const unsigned char *x
, unsigned length
);
414 void cta_displayid_type_10(const unsigned char *x
, unsigned length
);
415 void cta_block(const unsigned char *x
, std::vector
<unsigned> &found_tags
);
416 void preparse_cta_block(unsigned char *x
);
417 void parse_cta_block(const unsigned char *x
);
418 void cta_resolve_svr(timings_ext
&t_ext
);
419 void cta_resolve_svrs();
420 void check_cta_blocks();
421 void cta_list_vics();
422 void cta_list_hdmi_vics();
423 void cta_list_rids();
424 void cta_list_rid_timings(unsigned list_rid
= 0);
426 void set_displayid_native_res(unsigned w
, unsigned h
);
427 void parse_digital_interface(const unsigned char *x
);
428 void parse_display_device(const unsigned char *x
);
429 void parse_display_caps(const unsigned char *x
);
430 void parse_display_xfer(const unsigned char *x
);
431 void parse_di_ext_block(const unsigned char *x
);
433 void check_displayid_datablock_revision(unsigned char hdr
,
434 unsigned char valid_flags
= 0,
435 unsigned char rev
= 0);
436 void parse_displayid_product_id(const unsigned char *x
);
437 std::string
product_type(unsigned char x
, bool heading
);
438 void parse_displayid_interface_features(const unsigned char *x
);
439 void parse_displayid_parameters(const unsigned char *x
);
440 void parse_displayid_parameters_v2(const unsigned char *x
, unsigned block_rev
);
441 void parse_displayid_display_intf(const unsigned char *x
);
442 void parse_displayid_color_characteristics(const unsigned char *x
);
443 void parse_displayid_transfer_characteristics(const unsigned char *x
);
444 void parse_displayid_stereo_display_intf(const unsigned char *x
);
445 void parse_displayid_type_1_7_timing(const unsigned char *x
,
446 bool type7
, unsigned block_rev
, bool is_cta
= false);
447 void parse_displayid_type_2_timing(const unsigned char *x
);
448 void parse_displayid_type_3_timing(const unsigned char *x
);
449 void parse_displayid_type_4_8_timing(unsigned char type
, unsigned short id
, bool is_cta
= false);
450 void parse_displayid_video_timing_range_limits(const unsigned char *x
);
451 void parse_displayid_string(const unsigned char *x
);
452 void parse_displayid_display_device(const unsigned char *x
);
453 void parse_displayid_intf_power_sequencing(const unsigned char *x
);
454 void parse_displayid_type_5_timing(const unsigned char *x
);
455 void parse_displayid_tiled_display_topology(const unsigned char *x
, bool is_v2
);
456 void parse_displayid_type_6_timing(const unsigned char *x
);
457 void parse_displayid_type_9_timing(const unsigned char *x
);
458 void parse_displayid_dynamic_video_timings_range_limits(const unsigned char *x
);
459 void parse_displayid_ContainerID(const unsigned char *x
);
460 void parse_displayid_adaptive_sync(const unsigned char *x
);
461 void parse_displayid_type_10_timing(const unsigned char *x
, unsigned sz
,
462 bool is_cta
= false);
463 void preparse_displayid_block(unsigned char *x
);
464 unsigned displayid_block(const unsigned version
, const unsigned char *x
, unsigned length
);
465 void parse_displayid_block(const unsigned char *x
);
466 void parse_displayid_vesa(const unsigned char *x
);
467 void parse_displayid_apple(const unsigned char *x
);
468 void parse_displayid_cta_data_block(const unsigned char *x
);
469 void check_displayid_blocks();
471 void parse_vtb_ext_block(const unsigned char *x
);
473 void parse_string_table(const unsigned char *x
);
474 void preparse_ls_ext_block(unsigned char *x
);
475 void parse_ls_ext_block(const unsigned char *x
);
477 void parse_block_map(const unsigned char *x
);
479 void preparse_extension(unsigned char *x
);
480 void parse_extension(const unsigned char *x
);
481 void print_preferred_timings();
482 void print_native_res();
486 static inline void add_str(std::string
&s
, const std::string
&add
)
490 else if (!add
.empty())
494 void msg(bool is_warn
, const char *fmt
, ...);
498 #define warn(fmt, ...) msg(true, fmt, __VA_ARGS__)
499 #define warn_once(fmt, ...) \
501 static bool shown_warn; \
504 msg(true, fmt, __VA_ARGS__); \
507 #define fail(fmt, ...) msg(false, fmt, __VA_ARGS__)
511 #define warn(fmt, args...) msg(true, fmt, ##args)
512 #define warn_once(fmt, args...) \
514 static bool shown_warn; \
517 msg(true, fmt, ##args); \
520 #define fail(fmt, args...) msg(false, fmt, ##args)
524 std::string
utohex(unsigned char x
);
525 std::string
ouitohex(unsigned oui
);
526 std::string
containerid2s(const unsigned char *x
);
527 bool memchk(const unsigned char *x
, unsigned len
, unsigned char v
= 0);
528 void hex_block(const char *prefix
, const unsigned char *x
, unsigned length
,
529 bool show_ascii
= true, unsigned step
= 16);
530 std::string
block_name(unsigned char block
);
531 void do_checksum(const char *prefix
, const unsigned char *x
, size_t len
, unsigned unused_bytes
= 0);
532 void replace_checksum(unsigned char *x
, size_t len
);
533 void calc_ratio(struct timings
*t
);
534 const char *oui_name(unsigned oui
, unsigned *ouinum
= NULL
);
535 unsigned gcd(unsigned a
, unsigned b
);
537 bool match_timings(const timings
&t1
, const timings
&t2
);
538 bool timings_close_match(const timings
&t1
, const timings
&t2
);
539 const struct timings
*find_dmt_id(unsigned char dmt_id
);
540 const struct timings
*close_match_to_dmt(const timings
&t
, unsigned &dmt
);
541 const struct timings
*find_vic_id(unsigned char vic
);
542 const struct cta_rid
*find_rid(unsigned char rid
);
543 const struct timings
*find_hdmi_vic_id(unsigned char hdmi_vic
);
544 const struct timings
*cta_close_match_to_vic(const timings
&t
, unsigned &vic
);
545 bool cta_matches_vic(const timings
&t
, unsigned &vic
);
546 unsigned char hdmi_vic_to_vic(unsigned char hdmi_vic
);
547 char *extract_string(const unsigned char *x
, unsigned len
);
549 #define oneoui(c,k,n) const unsigned kOUI_##k = __LINE__<<12;