summaryrefslogtreecommitdiff
path: root/src/backends/edid-parse.c
blob: 6c387842916361c23316fa411361d6fa9241e3d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
 * Copyright 2007 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/* Author: Soren Sandmann <sandmann@redhat.com> */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <glib.h>

#ifdef HAVE_LIBDISPLAY_INFO
#include <libdisplay-info/edid.h>
#include <libdisplay-info/info.h>
#endif

#include "backends/edid.h"

/* VESA E-EDID */
#define EDID_BLOCK_LENGTH   128
#define EDID_EXT_FLAG_ADDR  0x7E
#define EDID_EXT_TAG_ADDR   0x00

/* VESA reserved IDs for extension blocks */
#define EDID_EXT_ID_CTA     0x02

/* CTA-861 extension block */
#define EDID_EXT_CTA_REVISION_ADDR                        0x01
#define EDID_EXT_CTA_DESCRIPTOR_OFFSET_ADDR               0x02
#define EDID_EXT_CTA_DATA_BLOCK_OFFSET                    0x04
#define EDID_EXT_CTA_TAG_EXTENDED                         0x07
#define EDID_EXT_CTA_TAG_EXTENDED_COLORIMETRY             0x0705
#define EDID_EXT_CTA_TAG_EXTENDED_HDR_STATIC_METADATA     0x0706

static int
get_bit (int in, int bit)
{
  return (in & (1 << bit)) >> bit;
}

static int
get_bits (int in, int begin, int end)
{
  int mask = (1 << (end - begin + 1)) - 1;

  return (in >> begin) & mask;
}

static gboolean
decode_header (const uint8_t *edid)
{
  if (memcmp (edid, "\x00\xff\xff\xff\xff\xff\xff\x00", 8) == 0)
    return TRUE;
  return FALSE;
}

static gboolean
decode_vendor_and_product_identification (const uint8_t *edid,
                                          MetaEdidInfo  *info)
{
  /* Manufacturer Code */
  info->manufacturer_code[0] = get_bits (edid[0x08], 2, 6);
  info->manufacturer_code[1] = get_bits (edid[0x08], 0, 1) << 3;
  info->manufacturer_code[1] |= get_bits (edid[0x09], 5, 7);
  info->manufacturer_code[2] = get_bits (edid[0x09], 0, 4);
  info->manufacturer_code[3] = '\0';

  info->manufacturer_code[0] += 'A' - 1;
  info->manufacturer_code[1] += 'A' - 1;
  info->manufacturer_code[2] += 'A' - 1;

  /* Product Code */
  info->product_code = edid[0x0b] << 8 | edid[0x0a];

  /* Serial Number */
  info->serial_number =
    edid[0x0c] | edid[0x0d] << 8 | edid[0x0e] << 16 | edid[0x0f] << 24;
  return TRUE;
}

static gboolean
decode_display_parameters (const uint8_t *edid,
                           MetaEdidInfo  *info)
{
  /* Gamma */
  if (edid[0x17] == 0xFF)
    info->gamma = -1.0;
  else
    info->gamma = (edid[0x17] + 100.0) / 100.0;

  return TRUE;
}

static double
decode_fraction (int high, int low)
{
  double result = 0.0;
  int i;

  high = (high << 2) | low;

  for (i = 0; i < 10; ++i)
    result += get_bit (high, i) * pow (2, i - 10);

  return result;
}

static gboolean
decode_color_characteristics (const uint8_t *edid,
                              MetaEdidInfo  *info)
{
  info->red_x = decode_fraction (edid[0x1b], get_bits (edid[0x19], 6, 7));
  info->red_y = decode_fraction (edid[0x1c], get_bits (edid[0x19], 5, 4));
  info->green_x = decode_fraction (edid[0x1d], get_bits (edid[0x19], 2, 3));
  info->green_y = decode_fraction (edid[0x1e], get_bits (edid[0x19], 0, 1));
  info->blue_x = decode_fraction (edid[0x1f], get_bits (edid[0x1a], 6, 7));
  info->blue_y = decode_fraction (edid[0x20], get_bits (edid[0x1a], 4, 5));
  info->white_x = decode_fraction (edid[0x21], get_bits (edid[0x1a], 2, 3));
  info->white_y = decode_fraction (edid[0x22], get_bits (edid[0x1a], 0, 1));

  return TRUE;
}

static void
decode_lf_string (const uint8_t *s,
                  int            n_chars,
                  char          *result)
{
  int i;
  for (i = 0; i < n_chars; ++i)
    {
      if (s[i] == 0x0a)
	{
          *result++ = '\0';
          break;
	}
      else if (s[i] == 0x00)
	{
          /* Convert embedded 0's to spaces */
          *result++ = ' ';
	}
      else
	{
          *result++ = s[i];
	}
    }
}

static void
decode_display_descriptor (const uint8_t *desc,
                           MetaEdidInfo  *info)
{
  switch (desc[0x03])
    {
    case 0xFC:
      decode_lf_string (desc + 5, 13, info->dsc_product_name);
      break;
    case 0xFF:
      decode_lf_string (desc + 5, 13, info->dsc_serial_number);
      break;
    }
}


static gboolean
decode_descriptors (const uint8_t *edid,
                    MetaEdidInfo  *info)
{
  int i;

  for (i = 0; i < 4; ++i)
    {
      int index = 0x36 + i * 18;

      if (edid[index + 0] == 0x00 && edid[index + 1] == 0x00)
        {
          decode_display_descriptor (edid + index, info);
        }
    }

  return TRUE;
}

static gboolean
decode_ext_cta_colorimetry (const uint8_t *data_block,
                            MetaEdidInfo  *info)
{
  /* CTA-861-H: Table 78 - Colorimetry Data Block (CDB) */
  info->colorimetry = (data_block[3] << 8) + data_block[2];
  return TRUE;
}

static gboolean
decode_ext_cta_hdr_static_metadata (const uint8_t *data_block,
                                    MetaEdidInfo  *info)
{
  /* CTA-861-H: Table 92 - HDR Static Metadata Data Block (HDR SMDB) */
  int size;

  info->hdr_static_metadata.available = TRUE;
  info->hdr_static_metadata.tf = data_block[2];
  info->hdr_static_metadata.sm = data_block[3];

  size = get_bits (data_block[0], 0, 5);
  if (size > 3)
    info->hdr_static_metadata.max_luminance = data_block[4];
  if (size > 4)
    info->hdr_static_metadata.max_fal = data_block[5];
  if (size > 5)
    info->hdr_static_metadata.min_luminance = data_block[6];

  return TRUE;
}

static gboolean
decode_ext_cta (const uint8_t *cta_block,
                MetaEdidInfo  *info)
{
  const uint8_t *data_block;
  uint8_t data_block_end;
  uint8_t data_block_offset;
  int size;
  int tag;

  /* The CTA extension block is a number of data blocks followed by a number
   * of (timing) descriptors. We only parse the data blocks. */

  /* CTA-861-H Table 58: CTA Extension Version 3 */
  data_block_end = cta_block[EDID_EXT_CTA_DESCRIPTOR_OFFSET_ADDR];
  data_block_offset = EDID_EXT_CTA_DATA_BLOCK_OFFSET;

  /* Table 58:
   * If d=0, then no detailed timing descriptors are provided, and no data is
   * provided in the data block collection */
  if (data_block_end == 0)
    return TRUE;

  /* Table 58:
   * If no data is provided in the data block collection, then d=4 */
  if (data_block_end == 4)
    return TRUE;

  if (data_block_end < 4)
    return FALSE;

  while (data_block_offset < data_block_end)
    {
      /* CTA-861-H 7.4: CTA Data Block Collection */
      data_block = cta_block + data_block_offset;
      size = get_bits (data_block[0], 0, 4) + 1;
      tag = get_bits (data_block[0], 5, 7);

      data_block_offset += size;

      /* CTA Data Block extended tag type is the second byte */
      if (tag == EDID_EXT_CTA_TAG_EXTENDED)
        tag = (tag << 8) + data_block[1];

      switch (tag)
        {
        case EDID_EXT_CTA_TAG_EXTENDED_COLORIMETRY:
          if (!decode_ext_cta_colorimetry (data_block, info))
            return FALSE;
          break;
        case EDID_EXT_CTA_TAG_EXTENDED_HDR_STATIC_METADATA:
          if (!decode_ext_cta_hdr_static_metadata (data_block, info))
            return FALSE;
          break;
        }
    }

  return TRUE;
}

static gboolean
decode_extensions (const uint8_t *edid,
                   MetaEdidInfo  *info)
{
  int blocks;
  int i;
  const uint8_t *block = NULL;

  blocks = edid[EDID_EXT_FLAG_ADDR];

  for (i = 0; i < blocks; i++)
    {
      block = edid + EDID_BLOCK_LENGTH * (i + 1);

      switch (block[EDID_EXT_TAG_ADDR])
        {
        case EDID_EXT_ID_CTA:
          if (!decode_ext_cta (block, info))
            return FALSE;
          break;
        }
    }

  return TRUE;
}

#ifdef HAVE_LIBDISPLAY_INFO
static void
decode_edid_descriptors (const struct di_edid                    *di_edid,
                         const struct di_edid_display_descriptor *desc,
                         MetaEdidInfo                            *info)
{
  enum di_edid_display_descriptor_tag desc_tag;

  desc_tag = di_edid_display_descriptor_get_tag (desc);

  switch (desc_tag)
    {
    case DI_EDID_DISPLAY_DESCRIPTOR_PRODUCT_SERIAL:
      info->dsc_serial_number = di_edid_display_descriptor_get_string (desc);
      break;
    case DI_EDID_DISPLAY_DESCRIPTOR_PRODUCT_NAME:
      info->dsc_product_name = di_edid_display_descriptor_get_string (desc);
      break;
    }
}

static void
decode_edid_colorimetry (const struct di_cta_colorimetry_block *colorimetry,
                         MetaEdidInfo                          *info)
{
  /* Colorimetry Data Block */
  if (colorimetry->xvycc_601)
    info->colorimetry |= META_EDID_COLORIMETRY_XVYCC601;
  if (colorimetry->xvycc_709)
    info->colorimetry |= META_EDID_COLORIMETRY_XVYCC709;
  if (colorimetry->sycc_601)
    info->colorimetry |= META_EDID_COLORIMETRY_SYCC601;
  if (colorimetry->opycc_601)
    info->colorimetry |= META_EDID_COLORIMETRY_OPYCC601;
  if (colorimetry->oprgb)
    info->colorimetry |= META_EDID_COLORIMETRY_OPRGB;
  if (colorimetry->bt2020_cycc)
    info->colorimetry |= META_EDID_COLORIMETRY_BT2020CYCC;
  if (colorimetry->bt2020_ycc)
    info->colorimetry |= META_EDID_COLORIMETRY_BT2020YCC;
  if (colorimetry->bt2020_rgb)
    info->colorimetry |= META_EDID_COLORIMETRY_BT2020RGB;
  if (colorimetry->st2113_rgb)
    info->colorimetry |= META_EDID_COLORIMETRY_ST2113RGB;
  if (colorimetry->ictcp)
    info->colorimetry |= META_EDID_COLORIMETRY_ICTCP;
}

static void
decode_edid_hdr_static_metadata (const struct di_cta_hdr_static_metadata_block *hdr,
                                 MetaEdidInfo                                  *info)
{
  /* HDR Static Metadata Block */
  if (hdr->eotfs->traditional_sdr)
    info->hdr_static_metadata.tf |= META_EDID_TF_TRADITIONAL_GAMMA_SDR;
  if (hdr->eotfs->traditional_hdr)
    info->hdr_static_metadata.tf |= META_EDID_TF_TRADITIONAL_GAMMA_HDR;
  if (hdr->eotfs->pq)
    info->hdr_static_metadata.tf |= META_EDID_TF_PQ;
  if (hdr->eotfs->hlg)
    info->hdr_static_metadata.tf |= META_EDID_TF_HLG;

  if (hdr->descriptors->type1)
    info->hdr_static_metadata.sm |= META_EDID_STATIC_METADATA_TYPE1;

  if (hdr->desired_content_max_luminance != 0)
    info->hdr_static_metadata.max_luminance = round (hdr->desired_content_max_luminance);
  if (hdr->desired_content_max_frame_avg_luminance != 0)
    info->hdr_static_metadata.max_fal = hdr->desired_content_max_frame_avg_luminance;
  if (hdr->desired_content_min_luminance != 0)
    info->hdr_static_metadata.min_luminance = round (hdr->desired_content_min_luminance);
}
static void
decode_edid_cta_ext (const struct di_edid_cta *cta,
                     MetaEdidInfo             *info)
{
  const struct di_cta_data_block *const *data_blks;
  const struct di_cta_data_block *data_blk;
  enum di_cta_data_block_tag data_blk_tag;
  const struct di_cta_colorimetry_block *colorimetry;
  const struct di_cta_hdr_static_metadata_block *hdr_static_metadata;

  data_blks = di_edid_cta_get_data_blocks (cta);
  for (data_index = 0; data_blks[data_index]!=NULL; data_index++)
    {
      data_blk = data_blks[data_index];
      data_blk_tag = di_cta_data_block_get_tag (data_blk);

      switch (data_blk_tag)
        {
        case DI_CTA_DATA_BLOCK_COLORIMETRY:
          colorimetry = di_cta_data_block_get_colorimetry (data_blk);
          g_assert (colorimetry);
          decode_edid_colorimetry (colorimetry, info)
          break;
        case DI_CTA_DATA_BLOCK_HDR_STATIC_METADATA:
          hdr_static_metadata = di_cta_data_block_get_hdr_static_metadata (data_blk);
          g_assert (hdr_static_metadata);
          decode_edid_hdr_static_metadata (hdr_static_metadata, info);
          break;
        }
    }
}

static void
decode_edid_extensions (const struct di_edid_ext *ext,
                        MetaEdidInfo             *info)
{
  enum di_edid_ext_tag ext_tag;
  const struct di_edid_cta *cta;
  ext_tag = di_edid_ext_get_tag (ext);

  switch (ext_tag)
    {
    case DI_EDID_EXT_CEA:
      cta = di_edid_ext_get_cta (ext);
      decode_edid_cta_ext (cta, info);
      break;
    }
}
#endif

static gboolean
decode_edid_info (const uint8_t *edid,
                  MetaEdidInfo  *info,
                  size_t         size)
{
#ifdef HAVE_LIBDISPLAY_INFO
  const struct di_edid *di_edid;
  struct di_info *edid_info;
  const struct di_edid_vendor_product *vendor_product;
  const struct di_edid_chromaticity_coords *chromaticity_coords;
  float gamma;
  const struct di_edid_display_descriptor *const *edid_descriptors;
  const struct di_edid_ext *const *extensions;

  edid_info = di_info_parse_edid (edid, size);

  if (!edid_info)
    {
      return FALSE;
    }

  di_edid = di_info_get_edid (edid_info);

  /* Vendor and Product identification */
  vendor_product = di_edid_get_vendor_product (di_edid);

  /* Manufacturer Code */
  strncpy (info->manufacturer_code, vendor_product->manufacturer,
           sizeof(info->manufacturer_code));

  /* Product Code */
  info->product_code = vendor_product->product;

  /* Serial Number */
  info->serial_number = vendor_product->serial;

  /* Color Characteristics */
  chromaticity_coords = di_edid_get_chromaticity_coords (di_edid);
  info->red_x = chromaticity_coords->red_x;
  info->red_y = chromaticity_coords->red_y;
  info->green_x = chromaticity_coords->green_x;
  info->green_y = chromaticity_coords->green_y;
  info->blue_x = chromaticity_coords->blue_x;
  info->blue_y = chromaticity_coords->blue_y;
  info->white_x = chromaticity_coords->white_x;
  info->white_y = chromaticity_coords->white_y;

  /* Gamma */
  gamma = di_edid_get_basic_gamma (di_edid);
  if (gamma != 0)
    info->gamma = gamma;
  else
    info->gamma = -1;

  /* Descriptors */
  edid_descriptors = di_edid_get_display_descriptors (di_edid);
  for (desc_index = 0; edid_descriptors[desc_index] != NULL; i++)
    {
      decode_edid_descriptors (di_edid, edid_descriptors[desc_index], info);
    }

  /* Extension Blocks */
  extensions = di_edid_get_extensions (di_edid);

  for (ext_index = 0; extensions[ext_index] != NULL; i++)
    {
      decode_edid_extensions (extensions[ext_index], info);
    }

  return TRUE;
#else
  return decode_header (edid) &&
         decode_vendor_and_product_identification (edid, info) &&
         decode_display_parameters (edid, info) &&
         decode_color_characteristics (edid, info) &&
         decode_descriptors (edid, info) &&
         decode_extensions (edid, info);
#endif
}

MetaEdidInfo *
meta_edid_info_new_parse (const uint8_t *edid,
                          size_t         size)
{
  MetaEdidInfo *info;

  info = g_new0 (MetaEdidInfo, 1);

  if (decode_edid_info (edid, info, size))
    {
      return info;
    }
  else
    {
      g_free (info);
      return NULL;
    }
}