2 * Copyright 2007 Red Hat, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 /* Author: Soren Sandmann <sandmann@redhat.com> */
24 #include "../../SDL_internal.h"
25 #include "SDL_stdinc.h"
37 get_bit (int in, int bit)
39 return (in & (1 << bit)) >> bit;
43 get_bits (int in, int begin, int end)
45 int mask = (1 << (end - begin + 1)) - 1;
47 return (in >> begin) & mask;
51 decode_header (const uchar *edid)
53 if (memcmp (edid, "\x00\xff\xff\xff\xff\xff\xff\x00", 8) == 0)
59 decode_vendor_and_product_identification (const uchar *edid, MonitorInfo *info)
63 /* Manufacturer Code */
64 info->manufacturer_code[0] = get_bits (edid[0x08], 2, 6);
65 info->manufacturer_code[1] = get_bits (edid[0x08], 0, 1) << 3;
66 info->manufacturer_code[1] |= get_bits (edid[0x09], 5, 7);
67 info->manufacturer_code[2] = get_bits (edid[0x09], 0, 4);
68 info->manufacturer_code[3] = '\0';
70 info->manufacturer_code[0] += 'A' - 1;
71 info->manufacturer_code[1] += 'A' - 1;
72 info->manufacturer_code[2] += 'A' - 1;
75 info->product_code = edid[0x0b] << 8 | edid[0x0a];
79 edid[0x0c] | edid[0x0d] << 8 | edid[0x0e] << 16 | edid[0x0f] << 24;
82 is_model_year = FALSE;
86 info->production_week = -1;
90 info->production_week = -1;
95 info->production_week = edid[0x10];
101 info->production_year = -1;
102 info->model_year = 1990 + edid[0x11];
106 info->production_year = 1990 + edid[0x11];
107 info->model_year = -1;
114 decode_edid_version (const uchar *edid, MonitorInfo *info)
116 info->major_version = edid[0x12];
117 info->minor_version = edid[0x13];
123 decode_display_parameters (const uchar *edid, MonitorInfo *info)
125 /* Digital vs Analog */
126 info->is_digital = get_bit (edid[0x14], 7);
128 if (info->is_digital)
132 static const int bit_depth[8] =
134 -1, 6, 8, 10, 12, 14, 16, -1
137 static const Interface interfaces[6] =
139 UNDEFINED, DVI, HDMI_A, HDMI_B, MDDI, DISPLAY_PORT
142 bits = get_bits (edid[0x14], 4, 6);
143 info->ad.digital.bits_per_primary = bit_depth[bits];
145 bits = get_bits (edid[0x14], 0, 3);
148 info->ad.digital.interface = interfaces[bits];
150 info->ad.digital.interface = UNDEFINED;
154 int bits = get_bits (edid[0x14], 5, 6);
156 static const double levels[][3] =
159 { 0.714, 0.286, 1.0 },
164 info->ad.analog.video_signal_level = levels[bits][0];
165 info->ad.analog.sync_signal_level = levels[bits][1];
166 info->ad.analog.total_signal_level = levels[bits][2];
168 info->ad.analog.blank_to_black = get_bit (edid[0x14], 4);
170 info->ad.analog.separate_hv_sync = get_bit (edid[0x14], 3);
171 info->ad.analog.composite_sync_on_h = get_bit (edid[0x14], 2);
172 info->ad.analog.composite_sync_on_green = get_bit (edid[0x14], 1);
174 info->ad.analog.serration_on_vsync = get_bit (edid[0x14], 0);
177 /* Screen Size / Aspect Ratio */
178 if (edid[0x15] == 0 && edid[0x16] == 0)
181 info->height_mm = -1;
182 info->aspect_ratio = -1.0;
184 else if (edid[0x16] == 0)
187 info->height_mm = -1;
188 info->aspect_ratio = 100.0 / (edid[0x15] + 99);
190 else if (edid[0x15] == 0)
193 info->height_mm = -1;
194 info->aspect_ratio = 100.0 / (edid[0x16] + 99);
195 info->aspect_ratio = 1/info->aspect_ratio; /* portrait */
199 info->width_mm = 10 * edid[0x15];
200 info->height_mm = 10 * edid[0x16];
204 if (edid[0x17] == 0xFF)
207 info->gamma = (edid[0x17] + 100.0) / 100.0;
210 info->standby = get_bit (edid[0x18], 7);
211 info->suspend = get_bit (edid[0x18], 6);
212 info->active_off = get_bit (edid[0x18], 5);
214 if (info->is_digital)
216 info->ad.digital.rgb444 = TRUE;
217 if (get_bit (edid[0x18], 3))
218 info->ad.digital.ycrcb444 = 1;
219 if (get_bit (edid[0x18], 4))
220 info->ad.digital.ycrcb422 = 1;
224 int bits = get_bits (edid[0x18], 3, 4);
225 ColorType color_type[4] =
227 MONOCHROME, RGB, OTHER_COLOR, UNDEFINED_COLOR
230 info->ad.analog.color_type = color_type[bits];
233 info->srgb_is_standard = get_bit (edid[0x18], 2);
235 /* In 1.3 this is called "has preferred timing" */
236 info->preferred_timing_includes_native = get_bit (edid[0x18], 1);
238 /* FIXME: In 1.3 this indicates whether the monitor accepts GTF */
239 info->continuous_frequency = get_bit (edid[0x18], 0);
244 decode_fraction (int high, int low)
249 high = (high << 2) | low;
251 for (i = 0; i < 10; ++i)
252 result += get_bit (high, i) * SDL_pow (2, i - 10);
258 decode_color_characteristics (const uchar *edid, MonitorInfo *info)
260 info->red_x = decode_fraction (edid[0x1b], get_bits (edid[0x19], 6, 7));
261 info->red_y = decode_fraction (edid[0x1c], get_bits (edid[0x19], 5, 4));
262 info->green_x = decode_fraction (edid[0x1d], get_bits (edid[0x19], 2, 3));
263 info->green_y = decode_fraction (edid[0x1e], get_bits (edid[0x19], 0, 1));
264 info->blue_x = decode_fraction (edid[0x1f], get_bits (edid[0x1a], 6, 7));
265 info->blue_y = decode_fraction (edid[0x20], get_bits (edid[0x1a], 4, 5));
266 info->white_x = decode_fraction (edid[0x21], get_bits (edid[0x1a], 2, 3));
267 info->white_y = decode_fraction (edid[0x22], get_bits (edid[0x1a], 0, 1));
273 decode_established_timings (const uchar *edid, MonitorInfo *info)
275 static const Timing established[][8] =
312 for (i = 0; i < 3; ++i)
314 for (j = 0; j < 8; ++j)
316 int byte = edid[0x23 + i];
318 if (get_bit (byte, j) && established[i][j].frequency != 0)
319 info->established[idx++] = established[i][j];
326 decode_standard_timings (const uchar *edid, MonitorInfo *info)
330 for (i = 0; i < 8; i++)
332 int first = edid[0x26 + 2 * i];
333 int second = edid[0x27 + 2 * i];
335 if (first != 0x01 && second != 0x01)
337 int w = 8 * (first + 31);
340 switch (get_bits (second, 6, 7))
342 case 0x00: h = (w / 16) * 10; break;
343 case 0x01: h = (w / 4) * 3; break;
344 case 0x02: h = (w / 5) * 4; break;
345 case 0x03: h = (w / 16) * 9; break;
348 info->standard[i].width = w;
349 info->standard[i].height = h;
350 info->standard[i].frequency = get_bits (second, 0, 5) + 60;
358 decode_lf_string (const uchar *s, int n_chars, char *result)
361 for (i = 0; i < n_chars; ++i)
368 else if (s[i] == 0x00)
370 /* Convert embedded 0's to spaces */
381 decode_display_descriptor (const uchar *desc,
387 decode_lf_string (desc + 5, 13, info->dsc_product_name);
390 decode_lf_string (desc + 5, 13, info->dsc_serial_number);
393 decode_lf_string (desc + 5, 13, info->dsc_string);
402 /* Timing Identifications */
405 /* Color Management */
411 /* Established Timings */
419 decode_detailed_timing (const uchar *timing,
420 DetailedTiming *detailed)
423 StereoType stereo[] =
425 NO_STEREO, NO_STEREO, FIELD_RIGHT, FIELD_LEFT,
426 TWO_WAY_RIGHT_ON_EVEN, TWO_WAY_LEFT_ON_EVEN,
427 FOUR_WAY_INTERLEAVED, SIDE_BY_SIDE
430 detailed->pixel_clock = (timing[0x00] | timing[0x01] << 8) * 10000;
431 detailed->h_addr = timing[0x02] | ((timing[0x04] & 0xf0) << 4);
432 detailed->h_blank = timing[0x03] | ((timing[0x04] & 0x0f) << 8);
433 detailed->v_addr = timing[0x05] | ((timing[0x07] & 0xf0) << 4);
434 detailed->v_blank = timing[0x06] | ((timing[0x07] & 0x0f) << 8);
435 detailed->h_front_porch = timing[0x08] | get_bits (timing[0x0b], 6, 7) << 8;
436 detailed->h_sync = timing[0x09] | get_bits (timing[0x0b], 4, 5) << 8;
437 detailed->v_front_porch =
438 get_bits (timing[0x0a], 4, 7) | get_bits (timing[0x0b], 2, 3) << 4;
440 get_bits (timing[0x0a], 0, 3) | get_bits (timing[0x0b], 0, 1) << 4;
441 detailed->width_mm = timing[0x0c] | get_bits (timing[0x0e], 4, 7) << 8;
442 detailed->height_mm = timing[0x0d] | get_bits (timing[0x0e], 0, 3) << 8;
443 detailed->right_border = timing[0x0f];
444 detailed->top_border = timing[0x10];
446 detailed->interlaced = get_bit (timing[0x11], 7);
449 bits = get_bits (timing[0x11], 5, 6) << 1 | get_bit (timing[0x11], 0);
450 detailed->stereo = stereo[bits];
455 detailed->digital_sync = get_bit (bits, 4);
456 if (detailed->digital_sync)
458 detailed->ad.digital.composite = !get_bit (bits, 3);
460 if (detailed->ad.digital.composite)
462 detailed->ad.digital.serrations = get_bit (bits, 2);
463 detailed->ad.digital.negative_vsync = FALSE;
467 detailed->ad.digital.serrations = FALSE;
468 detailed->ad.digital.negative_vsync = !get_bit (bits, 2);
471 detailed->ad.digital.negative_hsync = !get_bit (bits, 0);
475 detailed->ad.analog.bipolar = get_bit (bits, 3);
476 detailed->ad.analog.serrations = get_bit (bits, 2);
477 detailed->ad.analog.sync_on_green = !get_bit (bits, 1);
482 decode_descriptors (const uchar *edid, MonitorInfo *info)
489 for (i = 0; i < 4; ++i)
491 int index = 0x36 + i * 18;
493 if (edid[index + 0] == 0x00 && edid[index + 1] == 0x00)
495 decode_display_descriptor (edid + index, info);
499 decode_detailed_timing (
500 edid + index, &(info->detailed_timings[timing_idx++]));
504 info->n_detailed_timings = timing_idx;
510 decode_check_sum (const uchar *edid,
516 for (i = 0; i < 128; ++i)
519 info->checksum = check;
523 decode_edid (const uchar *edid)
525 MonitorInfo *info = calloc (1, sizeof (MonitorInfo));
527 decode_check_sum (edid, info);
529 if (!decode_header (edid) ||
530 !decode_vendor_and_product_identification (edid, info) ||
531 !decode_edid_version (edid, info) ||
532 !decode_display_parameters (edid, info) ||
533 !decode_color_characteristics (edid, info) ||
534 !decode_established_timings (edid, info) ||
535 !decode_standard_timings (edid, info) ||
536 !decode_descriptors (edid, info)) {
547 return v? "yes" : "no";
551 dump_monitor_info (MonitorInfo *info)
555 printf ("Checksum: %d (%s)\n",
556 info->checksum, info->checksum? "incorrect" : "correct");
557 printf ("Manufacturer Code: %s\n", info->manufacturer_code);
558 printf ("Product Code: 0x%x\n", info->product_code);
559 printf ("Serial Number: %u\n", info->serial_number);
561 if (info->production_week != -1)
562 printf ("Production Week: %d\n", info->production_week);
564 printf ("Production Week: unspecified\n");
566 if (info->production_year != -1)
567 printf ("Production Year: %d\n", info->production_year);
569 printf ("Production Year: unspecified\n");
571 if (info->model_year != -1)
572 printf ("Model Year: %d\n", info->model_year);
574 printf ("Model Year: unspecified\n");
576 printf ("EDID revision: %d.%d\n", info->major_version, info->minor_version);
578 printf ("Display is %s\n", info->is_digital? "digital" : "analog");
579 if (info->is_digital)
581 const char *interface;
582 if (info->ad.digital.bits_per_primary != -1)
583 printf ("Bits Per Primary: %d\n", info->ad.digital.bits_per_primary);
585 printf ("Bits Per Primary: undefined\n");
587 switch (info->ad.digital.interface)
589 case DVI: interface = "DVI"; break;
590 case HDMI_A: interface = "HDMI-a"; break;
591 case HDMI_B: interface = "HDMI-b"; break;
592 case MDDI: interface = "MDDI"; break;
593 case DISPLAY_PORT: interface = "DisplayPort"; break;
594 case UNDEFINED: interface = "undefined"; break;
595 default: interface = "unknown"; break;
597 printf ("Interface: %s\n", interface);
599 printf ("RGB 4:4:4: %s\n", yesno (info->ad.digital.rgb444));
600 printf ("YCrCb 4:4:4: %s\n", yesno (info->ad.digital.ycrcb444));
601 printf ("YCrCb 4:2:2: %s\n", yesno (info->ad.digital.ycrcb422));
606 printf ("Video Signal Level: %f\n", info->ad.analog.video_signal_level);
607 printf ("Sync Signal Level: %f\n", info->ad.analog.sync_signal_level);
608 printf ("Total Signal Level: %f\n", info->ad.analog.total_signal_level);
610 printf ("Blank to Black: %s\n",
611 yesno (info->ad.analog.blank_to_black));
612 printf ("Separate HV Sync: %s\n",
613 yesno (info->ad.analog.separate_hv_sync));
614 printf ("Composite Sync on H: %s\n",
615 yesno (info->ad.analog.composite_sync_on_h));
616 printf ("Serration on VSync: %s\n",
617 yesno (info->ad.analog.serration_on_vsync));
619 switch (info->ad.analog.color_type)
621 case UNDEFINED_COLOR: s = "undefined"; break;
622 case MONOCHROME: s = "monochrome"; break;
623 case RGB: s = "rgb"; break;
624 case OTHER_COLOR: s = "other color"; break;
625 default: s = "unknown"; break;
628 printf ("Color: %s\n", s);
631 if (info->width_mm == -1)
632 printf ("Width: undefined\n");
634 printf ("Width: %d mm\n", info->width_mm);
636 if (info->height_mm == -1)
637 printf ("Height: undefined\n");
639 printf ("Height: %d mm\n", info->height_mm);
641 if (info->aspect_ratio > 0)
642 printf ("Aspect Ratio: %f\n", info->aspect_ratio);
644 printf ("Aspect Ratio: undefined\n");
646 if (info->gamma >= 0)
647 printf ("Gamma: %f\n", info->gamma);
649 printf ("Gamma: undefined\n");
651 printf ("Standby: %s\n", yesno (info->standby));
652 printf ("Suspend: %s\n", yesno (info->suspend));
653 printf ("Active Off: %s\n", yesno (info->active_off));
655 printf ("SRGB is Standard: %s\n", yesno (info->srgb_is_standard));
656 printf ("Preferred Timing Includes Native: %s\n",
657 yesno (info->preferred_timing_includes_native));
658 printf ("Continuous Frequency: %s\n", yesno (info->continuous_frequency));
660 printf ("Red X: %f\n", info->red_x);
661 printf ("Red Y: %f\n", info->red_y);
662 printf ("Green X: %f\n", info->green_x);
663 printf ("Green Y: %f\n", info->green_y);
664 printf ("Blue X: %f\n", info->blue_x);
665 printf ("Blue Y: %f\n", info->blue_y);
666 printf ("White X: %f\n", info->white_x);
667 printf ("White Y: %f\n", info->white_y);
669 printf ("Established Timings:\n");
671 for (i = 0; i < 24; ++i)
673 Timing *timing = &(info->established[i]);
675 if (timing->frequency == 0)
678 printf (" %d x %d @ %d Hz\n",
679 timing->width, timing->height, timing->frequency);
683 printf ("Standard Timings:\n");
684 for (i = 0; i < 8; ++i)
686 Timing *timing = &(info->standard[i]);
688 if (timing->frequency == 0)
691 printf (" %d x %d @ %d Hz\n",
692 timing->width, timing->height, timing->frequency);
695 for (i = 0; i < info->n_detailed_timings; ++i)
697 DetailedTiming *timing = &(info->detailed_timings[i]);
700 printf ("Timing%s: \n",
701 (i == 0 && info->preferred_timing_includes_native)?
702 " (Preferred)" : "");
703 printf (" Pixel Clock: %d\n", timing->pixel_clock);
704 printf (" H Addressable: %d\n", timing->h_addr);
705 printf (" H Blank: %d\n", timing->h_blank);
706 printf (" H Front Porch: %d\n", timing->h_front_porch);
707 printf (" H Sync: %d\n", timing->h_sync);
708 printf (" V Addressable: %d\n", timing->v_addr);
709 printf (" V Blank: %d\n", timing->v_blank);
710 printf (" V Front Porch: %d\n", timing->v_front_porch);
711 printf (" V Sync: %d\n", timing->v_sync);
712 printf (" Width: %d mm\n", timing->width_mm);
713 printf (" Height: %d mm\n", timing->height_mm);
714 printf (" Right Border: %d\n", timing->right_border);
715 printf (" Top Border: %d\n", timing->top_border);
716 switch (timing->stereo)
719 case NO_STEREO: s = "No Stereo"; break;
720 case FIELD_RIGHT: s = "Field Sequential, Right on Sync"; break;
721 case FIELD_LEFT: s = "Field Sequential, Left on Sync"; break;
722 case TWO_WAY_RIGHT_ON_EVEN: s = "Two-way, Right on Even"; break;
723 case TWO_WAY_LEFT_ON_EVEN: s = "Two-way, Left on Even"; break;
724 case FOUR_WAY_INTERLEAVED: s = "Four-way Interleaved"; break;
725 case SIDE_BY_SIDE: s = "Side-by-Side"; break;
727 printf (" Stereo: %s\n", s);
729 if (timing->digital_sync)
731 printf (" Digital Sync:\n");
732 printf (" composite: %s\n", yesno (timing->ad.digital.composite));
733 printf (" serrations: %s\n", yesno (timing->ad.digital.serrations));
734 printf (" negative vsync: %s\n",
735 yesno (timing->ad.digital.negative_vsync));
736 printf (" negative hsync: %s\n",
737 yesno (timing->ad.digital.negative_hsync));
741 printf (" Analog Sync:\n");
742 printf (" bipolar: %s\n", yesno (timing->ad.analog.bipolar));
743 printf (" serrations: %s\n", yesno (timing->ad.analog.serrations));
744 printf (" sync on green: %s\n", yesno (
745 timing->ad.analog.sync_on_green));
749 printf ("Detailed Product information:\n");
750 printf (" Product Name: %s\n", info->dsc_product_name);
751 printf (" Serial Number: %s\n", info->dsc_serial_number);
752 printf (" Unspecified String: %s\n", info->dsc_string);