ovr_sdk

view 3rdParty/EDID/edid.cpp @ 0:1b39a1b46319

initial 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 06:51:16 +0200
parents
children
line source
1 /*
2 * Copyright 2007 Red Hat, Inc.
3 *
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:
10 *
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
13 * Software.
14 *
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.
21 */
23 /* Author: Soren Sandmann <sandmann@redhat.com> */
24 #include "edid.h"
25 #include <stdint.h>
26 #include <math.h>
27 #include <memory.h>
28 #include <X11/Xatom.h>
31 static int get_bit(int in, int bit) {
32 return (in & (1 << bit)) >> bit;
33 }
35 static int get_bits(int in, int begin, int end) {
36 int mask = (1 << (end - begin + 1)) - 1;
38 return (in >> begin) & mask;
39 }
41 static bool decode_header(const uint8_t *edid) {
42 if (memcmp(edid, "\x00\xff\xff\xff\xff\xff\xff\x00", 8) == 0)
43 return true;
44 return false;
45 }
47 static int decode_vendor_and_product_identification(const uint8_t *edid, MonitorInfo *info) {
49 /* Manufacturer Code */
50 info->manufacturer_code[0] = get_bits(edid[0x08], 2, 6);
51 info->manufacturer_code[1] = get_bits(edid[0x08], 0, 1) << 3;
52 info->manufacturer_code[1] |= get_bits(edid[0x09], 5, 7);
53 info->manufacturer_code[2] = get_bits(edid[0x09], 0, 4);
54 info->manufacturer_code[3] = '\0';
56 info->manufacturer_code[0] += 'A' - 1;
57 info->manufacturer_code[1] += 'A' - 1;
58 info->manufacturer_code[2] += 'A' - 1;
60 /* Product Code */
61 info->product_code = edid[0x0b] << 8 | edid[0x0a];
63 /* Serial Number */
64 info->serial_number = edid[0x0c] | edid[0x0d] << 8 | edid[0x0e] << 16 | edid[0x0f] << 24;
66 /* Week and Year */
67 bool is_model_year = false;
68 switch (edid[0x10]) {
69 case 0x00:
70 info->production_week = -1;
71 break;
73 case 0xff:
74 info->production_week = -1;
75 is_model_year = true;
76 break;
78 default:
79 info->production_week = edid[0x10];
80 break;
81 }
83 if (is_model_year) {
84 info->production_year = -1;
85 info->model_year = 1990 + edid[0x11];
86 } else {
87 info->production_year = 1990 + edid[0x11];
88 info->model_year = -1;
89 }
91 return true;
92 }
94 static bool decode_edid_version(const uint8_t *edid, MonitorInfo *info) {
95 info->major_version = edid[0x12];
96 info->minor_version = edid[0x13];
97 return true;
98 }
100 static bool decode_display_parameters(const uint8_t *edid, MonitorInfo *info) {
101 /* Digital vs Analog */
102 info->is_digital = get_bit(edid[0x14], 7);
104 if (info->is_digital) {
105 static const int bit_depth[8] = { -1, 6, 8, 10, 12, 14, 16, -1 };
106 static const Interface interfaces[6] = { UNDEFINED, DVI, HDMI_A, HDMI_B, MDDI, DISPLAY_PORT };
108 int bits = get_bits(edid[0x14], 4, 6);
109 info->connector.digital.bits_per_primary = bit_depth[bits];
111 bits = get_bits(edid[0x14], 0, 3);
112 if (bits <= 5)
113 info->connector.digital.interface = interfaces[bits];
114 else
115 info->connector.digital.interface = UNDEFINED;
116 } else {
117 int bits = get_bits(edid[0x14], 5, 6);
118 static const double levels[][3] = { //
119 { 0.7, 0.3, 1.0 }, //
120 { 0.714, 0.286, 1.0 }, //
121 { 1.0, 0.4, 1.4 }, //
122 { 0.7, 0.0, 0.7 }, //
123 };
125 info->connector.analog.video_signal_level = levels[bits][0];
126 info->connector.analog.sync_signal_level = levels[bits][1];
127 info->connector.analog.total_signal_level = levels[bits][2];
128 info->connector.analog.blank_to_black = get_bit(edid[0x14], 4);
129 info->connector.analog.separate_hv_sync = get_bit(edid[0x14], 3);
130 info->connector.analog.composite_sync_on_h = get_bit(edid[0x14], 2);
131 info->connector.analog.composite_sync_on_green = get_bit(edid[0x14], 1);
132 info->connector.analog.serration_on_vsync = get_bit(edid[0x14], 0);
133 }
135 /* Screen Size / Aspect Ratio */
136 if (edid[0x15] == 0 && edid[0x16] == 0) {
137 info->width_mm = -1;
138 info->height_mm = -1;
139 info->aspect_ratio = -1.0;
140 } else if (edid[0x16] == 0) {
141 info->width_mm = -1;
142 info->height_mm = -1;
143 info->aspect_ratio = 100.0 / (edid[0x15] + 99);
144 } else if (edid[0x15] == 0) {
145 info->width_mm = -1;
146 info->height_mm = -1;
147 info->aspect_ratio = 100.0 / (edid[0x16] + 99);
148 info->aspect_ratio = 1 / info->aspect_ratio; /* portrait */
149 } else {
150 info->width_mm = 10 * edid[0x15];
151 info->height_mm = 10 * edid[0x16];
152 }
154 /* Gamma */
155 if (edid[0x17] == 0xFF)
156 info->gamma = -1.0;
157 else
158 info->gamma = (edid[0x17] + 100.0) / 100.0;
160 /* Features */
161 info->standby = get_bit(edid[0x18], 7);
162 info->suspend = get_bit(edid[0x18], 6);
163 info->active_off = get_bit(edid[0x18], 5);
165 if (info->is_digital) {
166 info->connector.digital.rgb444 = 1;
167 if (get_bit(edid[0x18], 3))
168 info->connector.digital.ycrcb444 = 1;
169 if (get_bit(edid[0x18], 4))
170 info->connector.digital.ycrcb422 = 1;
171 } else {
172 int bits = get_bits(edid[0x18], 3, 4);
173 ColorType color_type[4] = { MONOCHROME, RGB, OTHER_COLOR, UNDEFINED_COLOR };
175 info->connector.analog.color_type = color_type[bits];
176 }
178 info->srgb_is_standard = get_bit(edid[0x18], 2);
180 /* In 1.3 this is called "has preferred timing" */
181 info->preferred_timing_includes_native = get_bit(edid[0x18], 1);
183 /* FIXME: In 1.3 this indicates whether the monitor accepts GTF */
184 info->continuous_frequency = get_bit(edid[0x18], 0);
185 return true;
186 }
188 static double decode_fraction(int high, int low) {
189 double result = 0.0;
190 high = (high << 2) | low;
191 for (int i = 0; i < 10; ++i)
192 result += get_bit(high, i) * pow(2, i - 10);
193 return result;
194 }
196 static bool decode_color_characteristics(const uint8_t *edid, MonitorInfo *info) {
197 info->red_x = decode_fraction(edid[0x1b], get_bits(edid[0x19], 6, 7));
198 info->red_y = decode_fraction(edid[0x1c], get_bits(edid[0x19], 5, 4));
199 info->green_x = decode_fraction(edid[0x1d], get_bits(edid[0x19], 2, 3));
200 info->green_y = decode_fraction(edid[0x1e], get_bits(edid[0x19], 0, 1));
201 info->blue_x = decode_fraction(edid[0x1f], get_bits(edid[0x1a], 6, 7));
202 info->blue_y = decode_fraction(edid[0x20], get_bits(edid[0x1a], 4, 5));
203 info->white_x = decode_fraction(edid[0x21], get_bits(edid[0x1a], 2, 3));
204 info->white_y = decode_fraction(edid[0x22], get_bits(edid[0x1a], 0, 1));
206 return true;
207 }
209 static bool decode_established_timings(const uint8_t *edid, MonitorInfo *info) {
210 static const Timing established[][8] = { //
211 { { 800, 600, 60 }, { 800, 600, 56 }, //
212 { 640, 480, 75 }, { 640, 480, 72 }, //
213 { 640, 480, 67 }, { 640, 480, 60 }, //
214 { 720, 400, 88 }, { 720, 400, 70 } }, //
215 { { 1280, 1024, 75 }, { 1024, 768, 75 }, //
216 { 1024, 768, 70 }, { 1024, 768, 60 }, //
217 { 1024, 768, 87 }, { 832, 624, 75 }, //
218 { 800, 600, 75 }, { 800, 600, 72 } }, //
219 { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, //
220 { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 1152, 870, 75 } }, //
221 };
223 int idx = 0;
224 for (int i = 0; i < 3; ++i) {
225 for (int j = 0; j < 8; ++j) {
226 int byte = edid[0x23 + i];
228 if (get_bit(byte, j) && established[i][j].frequency != 0)
229 info->established[idx++] = established[i][j];
230 }
231 }
232 return true;
233 }
235 static bool decode_standard_timings(const uint8_t *edid, MonitorInfo *info) {
236 int i;
238 for (i = 0; i < 8; i++) {
239 int first = edid[0x26 + 2 * i];
240 int second = edid[0x27 + 2 * i];
242 if (first != 0x01 && second != 0x01) {
243 int w = 8 * (first + 31);
244 int h = 0;
246 switch (get_bits(second, 6, 7)) {
247 case 0x00:
248 h = (w / 16) * 10;
249 break;
250 case 0x01:
251 h = (w / 4) * 3;
252 break;
253 case 0x02:
254 h = (w / 5) * 4;
255 break;
256 case 0x03:
257 h = (w / 16) * 9;
258 break;
259 }
261 info->standard[i].width = w;
262 info->standard[i].height = h;
263 info->standard[i].frequency = get_bits(second, 0, 5) + 60;
264 }
265 }
267 return true;
268 }
270 static void decode_lf_string(const uint8_t *s, int n_chars, char *result) {
271 int i;
272 for (i = 0; i < n_chars; ++i) {
273 if (s[i] == 0x0a) {
274 *result++ = '\0';
275 break;
276 } else if (s[i] == 0x00) {
277 /* Convert embedded 0's to spaces */
278 *result++ = ' ';
279 } else {
280 *result++ = s[i];
281 }
282 }
283 }
285 static void decode_display_descriptor(const uint8_t *desc, MonitorInfo *info) {
286 switch (desc[0x03]) {
287 case 0xFC:
288 decode_lf_string(desc + 5, 13, info->dsc_product_name);
289 break;
290 case 0xFF:
291 decode_lf_string(desc + 5, 13, info->dsc_serial_number);
292 break;
293 case 0xFE:
294 decode_lf_string(desc + 5, 13, info->dsc_string);
295 break;
296 case 0xFD:
297 /* Range Limits */
298 break;
299 case 0xFB:
300 /* Color Point */
301 break;
302 case 0xFA:
303 /* Timing Identifications */
304 break;
305 case 0xF9:
306 /* Color Management */
307 break;
308 case 0xF8:
309 /* Timing Codes */
310 break;
311 case 0xF7:
312 /* Established Timings */
313 break;
314 case 0x10:
315 break;
316 }
317 }
319 static void decode_detailed_timing(const uint8_t *timing, DetailedTiming *detailed) {
320 int bits;
321 StereoType stereo[] = { //
322 NO_STEREO, NO_STEREO, //
323 FIELD_RIGHT, FIELD_LEFT, //
324 TWO_WAY_RIGHT_ON_EVEN, TWO_WAY_LEFT_ON_EVEN, //
325 FOUR_WAY_INTERLEAVED, //
326 SIDE_BY_SIDE //
327 };
329 detailed->pixel_clock = (timing[0x00] | timing[0x01] << 8) * 10000;
330 detailed->h_addr = timing[0x02] | ((timing[0x04] & 0xf0) << 4);
331 detailed->h_blank = timing[0x03] | ((timing[0x04] & 0x0f) << 8);
332 detailed->v_addr = timing[0x05] | ((timing[0x07] & 0xf0) << 4);
333 detailed->v_blank = timing[0x06] | ((timing[0x07] & 0x0f) << 8);
334 detailed->h_front_porch = timing[0x08] | get_bits(timing[0x0b], 6, 7) << 8;
335 detailed->h_sync = timing[0x09] | get_bits(timing[0x0b], 4, 5) << 8;
336 detailed->v_front_porch = get_bits(timing[0x0a], 4, 7) | get_bits(timing[0x0b], 2, 3) << 4;
337 detailed->v_sync = get_bits(timing[0x0a], 0, 3) | get_bits(timing[0x0b], 0, 1) << 4;
338 detailed->width_mm = timing[0x0c] | get_bits(timing[0x0e], 4, 7) << 8;
339 detailed->height_mm = timing[0x0d] | get_bits(timing[0x0e], 0, 3) << 8;
340 detailed->right_border = timing[0x0f];
341 detailed->top_border = timing[0x10];
342 detailed->interlaced = get_bit(timing[0x11], 7);
344 /* Stereo */
345 bits = get_bits(timing[0x11], 5, 6) << 1 | get_bit(timing[0x11], 0);
346 detailed->stereo = stereo[bits];
348 /* Sync */
349 bits = timing[0x11];
351 detailed->digital_sync = get_bit(bits, 4);
352 if (detailed->digital_sync) {
353 detailed->connector.digital.composite = !get_bit(bits, 3);
354 if (detailed->connector.digital.composite) {
355 detailed->connector.digital.serrations = get_bit(bits, 2);
356 detailed->connector.digital.negative_vsync = 0;
357 } else {
358 detailed->connector.digital.serrations = 0;
359 detailed->connector.digital.negative_vsync = !get_bit(bits, 2);
360 }
361 detailed->connector.digital.negative_hsync = !get_bit(bits, 0);
362 } else {
363 detailed->connector.analog.bipolar = get_bit(bits, 3);
364 detailed->connector.analog.serrations = get_bit(bits, 2);
365 detailed->connector.analog.sync_on_green = !get_bit(bits, 1);
366 }
367 }
369 static bool decode_descriptors(const uint8_t *edid, MonitorInfo *info) {
370 int timing_idx = 0;
371 for (int i = 0; i < 4; ++i) {
372 int index = 0x36 + i * 18;
373 if (edid[index + 0] == 0x00 && edid[index + 1] == 0x00) {
374 decode_display_descriptor(edid + index, info);
375 } else {
376 decode_detailed_timing(edid + index, &(info->detailed_timings[timing_idx++]));
377 }
378 }
379 info->n_detailed_timings = timing_idx;
380 return true;
381 }
383 static void decode_check_sum(const uint8_t *edid, MonitorInfo *info) {
384 uint8_t check = 0;
385 for (int i = 0; i < 128; ++i)
386 check += edid[i];
387 info->checksum = check;
388 }
390 MonitorInfo * decode_edid(const uint8_t *edid) {
391 MonitorInfo *info = new MonitorInfo();
392 decode_check_sum(edid, info);
393 if (decode_header(edid) && //
394 decode_vendor_and_product_identification(edid, info) && //
395 decode_edid_version(edid, info) && //
396 decode_display_parameters(edid, info) && //
397 decode_color_characteristics(edid, info) && //
398 decode_established_timings(edid, info) && //
399 decode_standard_timings(edid, info) && //
400 decode_descriptors(edid, info)) {
401 return info;
402 } else {
403 delete info;
404 return 0;
405 }
406 }
408 static uint8_t * get_property(Display *dpy, RROutput output, Atom atom, int *len) {
409 unsigned char *prop;
410 int actual_format;
411 unsigned long nitems, bytes_after;
412 Atom actual_type;
413 uint8_t *result = NULL;
415 XRRGetOutputProperty(dpy, output, atom, 0, 100, False, False,
416 AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &prop);
418 if (actual_type == XA_INTEGER && actual_format == 8) {
419 result = new uint8_t[nitems];
420 memcpy(result, prop, nitems);
421 if (len)
422 *len = nitems;
423 }
424 XFree(prop);
425 return result;
426 }
428 MonitorInfo * read_edid_data(Display * disp, RROutput id) {
429 int len;
430 Atom edid_atom = XInternAtom(disp, "EDID", false);
431 uint8_t *edid = get_property(disp, id, edid_atom, &len);
432 if (!edid) {
433 edid_atom = XInternAtom(disp, "EDID_DATA", false);
434 edid = get_property(disp, id, edid_atom, &len);
435 }
437 MonitorInfo * result = 0;
438 if (edid) {
439 if (len % 128 == 0) {
440 result = decode_edid(edid);
441 }
442 delete[] edid;
443 }
445 return result;
446 }